Skip to content

Commit 155a3ee

Browse files
committed
Powers of Two
1 parent 93c4822 commit 155a3ee

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/easy/PowersOfTwo.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package easy;
2+
3+
/**
4+
* Have the function PowersofTwo(num) take the num parameter being passed
5+
* which will be an integer and return the string true if it's a power of two.
6+
* If it's not return the string false.
7+
* For example if the input is 16 then your program should return the string true
8+
* but if the input is 22 then the output should be the string false.
9+
*/
10+
public class PowersOfTwo {
11+
12+
/**
13+
* Powers of Two function.
14+
*
15+
* @param num input number
16+
* @return the string true if it's a power of two.
17+
*/
18+
private static String powerOfTwo(int num) {
19+
int bitwise = num & num - 1;
20+
return num != 0 && bitwise == 0 ? "true" : "false";
21+
}
22+
23+
/**
24+
* Entry point.
25+
*
26+
* @param args command line arguments
27+
*/
28+
public static void main(String[] args) {
29+
var result1 = powerOfTwo(15);
30+
System.out.println(result1);
31+
var result2 = powerOfTwo(64);
32+
System.out.println(result2);
33+
}
34+
35+
}

0 commit comments

Comments
 (0)