forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPowerOf4.java
42 lines (39 loc) · 1.03 KB
/
PowerOf4.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
31
32
33
34
35
36
37
38
39
40
41
42
package com.rampatra.bits;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 6/9/15
* @time: 12:56 PM
*/
public class PowerOf4 {
/**
* Determines whether any +ve number
* is power of 4 or not.
*
* @param n
* @return
*/
public static boolean isPowerOf4(int n) {
int zeroBitCount = 0;
// first check whether only 1 bit is set
if (n > 0 && (n & (n - 1)) == 0) {
// count no. of unset bits after the set bit
while (n > 1) {
zeroBitCount++;
n >>= 1;
}
// if no. of unset bits are even then its a power of 4
return zeroBitCount % 2 == 0;
}
return false;
}
public static void main(String[] args) {
System.out.println(isPowerOf4(0));
System.out.println(isPowerOf4(1));
System.out.println(isPowerOf4(4));
System.out.println(isPowerOf4(16));
System.out.println(isPowerOf4(18));
System.out.println(isPowerOf4(64));
}
}