-
-
Notifications
You must be signed in to change notification settings - Fork 317
/
Debugger.java
30 lines (27 loc) · 863 Bytes
/
Debugger.java
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
package com.ctci.bitmanipulation;
/**
* @author rampatra
* @since 2019-03-17
*/
public class Debugger {
/**
* If after un-setting the least significant bit in n, it becomes 0 then it implies that it has only set bit. This
* can also imply that n is a power of 2.
*
* @param n input integer
* @return {@code true} if n has only set bit, {@code false} otherwise.
*/
private static boolean hasOneSetBit(int n) {
// edge case
if (n == 0) {
return false;
}
return (n & (n - 1)) == 0; // (n & (n - 1)) un-sets the least significant bit
}
public static void main(String[] args) {
System.out.println(hasOneSetBit(0));
System.out.println(hasOneSetBit(2));
System.out.println(hasOneSetBit(16));
System.out.println(hasOneSetBit(10));
}
}