File tree Expand file tree Collapse file tree 3 files changed +66
-13
lines changed
main/java/com/thealgorithms/bitmanipulation
test/java/com/thealgorithms/bitmanipulation Expand file tree Collapse file tree 3 files changed +66
-13
lines changed Original file line number Diff line number Diff line change 1- package bitmanipulation ;
1+ package com . thealgorithms . bitmanipulation ;
22
3- public class PowerOfFour {
4- public static boolean isPowerOfFour (int n ) {
5- // A power of 4 has only one bit set and that bit is at an even position
6- return n > 0 && (n & (n - 1 )) == 0 && (n & 0x55555555 ) != 0 ;
3+ /**
4+ * This class provides a method to check if a given number is a power of four.
5+ */
6+ public final class PowerOfFour {
7+
8+ // Private constructor to prevent instantiation
9+ private PowerOfFour () {
10+ throw new AssertionError ("Cannot instantiate utility class" );
711 }
812
9- public static void main (String [] args ) {
10- int num = 64 ; // change to test other numbers
11- if (isPowerOfFour (num ))
12- System .out .println (num + " is a power of 4." );
13- else
14- System .out .println (num + " is NOT a power of 4." );
13+ /**
14+ * Checks whether the given integer is a power of four.
15+ *
16+ * @param n the number to check
17+ * @return true if n is a power of four, false otherwise
18+ */
19+ public static boolean isPowerOfFour (int n ) {
20+ if (n <= 0 ) {
21+ return false ;
22+ } else {
23+ return (n & (n - 1 )) == 0 && (n & 0x55555555 ) != 0 ;
24+ }
1525 }
1626}
Original file line number Diff line number Diff line change 11package com .thealgorithms .bitmanipulation ;
22
3- public class PowerOfFour {
3+ /**
4+ * This class provides a method to check if a given number is a power of four.
5+ */
6+ public final class PowerOfFour {
47
8+ // Private constructor to prevent instantiation
59 private PowerOfFour () {
6- throw new IllegalStateException ( "Utility class" );
10+ throw new AssertionError ( "Cannot instantiate utility class" );
711 }
812
13+ /**
14+ * Checks whether the given integer is a power of four.
15+ *
16+ * @param n the number to check
17+ * @return true if n is a power of four, false otherwise
18+ */
919 public static boolean isPowerOfFour (int n ) {
1020 if (n <= 0 ) {
1121 return false ;
Original file line number Diff line number Diff line change 1+ package com .thealgorithms .bitmanipulation ;
2+
3+ import org .junit .jupiter .api .Assertions ;
4+ import org .junit .jupiter .api .Test ;
5+
6+ /**
7+ * Unit tests for PowerOfFour class.
8+ */
9+ public final class PowerOfFourTest {
10+
11+ @ Test
12+ void testPowerOfFourTrueCases () {
13+ Assertions .assertTrue (PowerOfFour .isPowerOfFour (1 ));
14+ Assertions .assertTrue (PowerOfFour .isPowerOfFour (4 ));
15+ Assertions .assertTrue (PowerOfFour .isPowerOfFour (16 ));
16+ Assertions .assertTrue (PowerOfFour .isPowerOfFour (64 ));
17+ }
18+
19+ @ Test
20+ void testPowerOfFourFalseCases () {
21+ Assertions .assertFalse (PowerOfFour .isPowerOfFour (0 ));
22+ Assertions .assertFalse (PowerOfFour .isPowerOfFour (2 ));
23+ Assertions .assertFalse (PowerOfFour .isPowerOfFour (8 ));
24+ Assertions .assertFalse (PowerOfFour .isPowerOfFour (12 ));
25+ }
26+
27+ @ Test
28+ void testNegativeNumbers () {
29+ Assertions .assertFalse (PowerOfFour .isPowerOfFour (-4 ));
30+ Assertions .assertFalse (PowerOfFour .isPowerOfFour (-16 ));
31+ }
32+ }
33+
You can’t perform that action at this time.
0 commit comments