-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathOnlySetBit.java
More file actions
35 lines (29 loc) · 809 Bytes
/
OnlySetBit.java
File metadata and controls
35 lines (29 loc) · 809 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*https://practice.geeksforgeeks.org/problems/find-position-of-set-bit3706/1*/
class Solution {
static int findPosition(int N) {
if (N == 0) return -1;
int count = 0;
boolean flag = false;
int data = 1;
//till the number exists
while (N > 0)
{
//increase the position count
++count;
//if the LSB is set
if ((N&data)==1)
{
//for the first time make flag true
if (!flag)
flag = true;
//on the second set bit return -1
else
return -1;
}
//right shift the number
N >>= 1;
}
//return the position count
return count;
}
}