forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path_231.java
More file actions
19 lines (16 loc) · 727 Bytes
/
_231.java
File metadata and controls
19 lines (16 loc) · 727 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.fishercoder.solutions;
/**231. Power of Two
*
* Given an integer, write a function to determine if it is a power of two.*/
public class _231 {
public boolean isPowerOfTwo(int n) {
//after writing out the binary representation of some numbers: 1,2,4,8,16,32, you can easily figure out that
//every number that is power of two has only one bit that is 1
//then we can apply that cool trick that we learned from {@link easy._191}: n&(n-1) which will clear the least significant bit in n to zero
return n > 0 && (n & (n - 1)) == 0;
}
public static void main(String... strings) {
_231 test = new _231();
System.out.println(test.isPowerOfTwo(14));
}
}