-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckPowerof4_.java
More file actions
34 lines (23 loc) · 1.06 KB
/
Copy pathCheckPowerof4_.java
File metadata and controls
34 lines (23 loc) · 1.06 KB
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
import java.util.Scanner;
public class CheckPowerof4_ {
public static void main(String[] arg) {
int test = 0; // Initialize a variable 'test' to 0
Scanner in = new Scanner(System.in);
// Create a Scanner object for user input
System.out.print("Input a positive integer: ");
// Prompt the user to input a positive integer
int n = in.nextInt(); // Read the user's input as an integer
if (n < 1) {
System.out.print(Boolean.toString(false)); // If n is less than 1, print "false" and set 'test' to 1
test = 1;
}
if ((n & (n - 1)) != 0) {
System.out.print(Boolean.toString(false)); // If n is not a power of 2, print "false" and set 'test' to 1
test = 1;
}
if (test == 0) {
System.out.print(Boolean.toString((n & 0x55555555) != 0)); // If 'test' is 0, check if n has odd bits set and print the result
}
System.out.print("\n"); // Print a new line
}
}