Skip to content

Commit bde04a6

Browse files
authored
Merge pull request #168 from FZYUAN-1/patch-1
Update Power-Of-Two.cpp
2 parents 605f19d + c3a9a5f commit bde04a6

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

June-LeetCoding-Challenge/08-Power-Of-Two/Power-Of-Two.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,25 @@ class Solution {
55
if (x == 0) return false;
66
return (x & (-x)) == x;
77
}
8-
};
8+
};
9+
10+
class Solution {
11+
public:
12+
int right_most_setbit(int num) {
13+
int pos = 0;
14+
// counting the position of first set bit
15+
for (int i = 0; i < 32; i++) {
16+
if (!(num & (1 << i)))
17+
pos++;
18+
else
19+
break;
20+
}
21+
return pos;
22+
}
23+
24+
bool isPowerOfTwo(int n) {
25+
if(n<=0) return false;
26+
else return right_most_setbit(n) == int(log2(n));
27+
28+
}
29+
};

0 commit comments

Comments
 (0)