From c3bdfb6ebb27c706629401634057344a9b003257 Mon Sep 17 00:00:00 2001 From: Suraj Kumar Sharma <72158521+surajsharma14@users.noreply.github.com> Date: Tue, 19 Sep 2023 23:22:11 +0530 Subject: [PATCH] Create 19-09-2023_Find first set bit --- September/19-09-2023_Find first set bit | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 September/19-09-2023_Find first set bit diff --git a/September/19-09-2023_Find first set bit b/September/19-09-2023_Find first set bit new file mode 100644 index 0000000..8199e88 --- /dev/null +++ b/September/19-09-2023_Find first set bit @@ -0,0 +1,17 @@ +class Solution +{ + public: + //Function to find position of first set bit in the given number. + unsigned int getFirstSetBit(int n) + { + unsigned int cnt=1; + while(n>0){ + if(n&1){ + return cnt; + } + n = n>>1; + cnt++; + } + return 0; + } +};