File tree Expand file tree Collapse file tree 2 files changed +28
-36
lines changed
Expand file tree Collapse file tree 2 files changed +28
-36
lines changed Original file line number Diff line number Diff line change 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+ }
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments