We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent b7a0562 commit 19176d9Copy full SHA for 19176d9
2 files changed
Maths/PowerOfTwoOrNot.java
@@ -0,0 +1,28 @@
1
+package Maths;
2
+
3
+/**
4
+ * A utility to check if a given number is power of two or not.
5
+ * For example 8,16 etc.
6
+ */
7
8
+public class PowerOfTwoOrNot {
9
10
+ public static void main(String[] args) {
11
+ assert !checkIfPowerOfTwoOrNot(0);
12
+ assert checkIfPowerOfTwoOrNot(1);
13
+ assert checkIfPowerOfTwoOrNot(8);
14
+ assert checkIfPowerOfTwoOrNot(16);
15
+ assert checkIfPowerOfTwoOrNot(1024);
16
+ }
17
18
19
+ /**
20
+ * Checks whether given number is power of two or not.
21
+ *
22
+ * @param number the number to check
23
+ * @return {@code true} if given number is power of two, otherwise {@code false}
24
25
+ public static boolean checkIfPowerOfTwoOrNot(int number) {
26
+ return number != 0 && ((number & (number - 1)) == 0);
27
28
+}
Others/PowerOfTwoOrNot.java
0 commit comments