Concept Flow - Check if Number is Even or Odd Using Bits
Start with number n
Extract least significant bit (LSB)
Is LSB 0?
Yes→Number is Even
No
Number is Odd
Check the last bit of the number; if it is 0, the number is even, else odd.
int n = 5; if ((n & 1) == 0) { System.out.println("Even"); } else { System.out.println("Odd"); }
| Step | Operation | Number (n) | Bitwise AND (n & 1) | Condition (== 0)? | Result |
|---|---|---|---|---|---|
| 1 | Start | 5 (binary 0101) | |||
| 2 | Calculate n & 1 | 5 | 1 (binary 0001) | ||
| 3 | Check if (n & 1) == 0 | 5 | 1 | False | Odd |
| 4 | Print result | 5 | Odd | ||
| 5 | End | 5 | Stop |
| Variable | Start | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|
| n | 5 | 5 | 5 | 5 |
| n & 1 | 1 | 1 | 1 | |
| Condition (n & 1 == 0) | False | False | ||
| Result | Odd |
Check if number is even or odd using bits: - Use (n & 1) to get last bit - If result == 0, number is even - Else, number is odd - Bitwise AND is faster than modulo - Works because LSB indicates parity
num & 1 check when determining if a number is even or odd?num & 1 performs a bitwise AND between num and 1, which isolates the least significant bit (rightmost bit) of num.num is even using bitwise operators?num & 1 isolates the least significant bit. If it equals 0, the number is even.(num & 1) == 0 to print "Even". Other options misuse bitwise OR, AND with 0, or XOR, which do not correctly check evenness.int num = 7;
if ((num & 1) == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}int num = 10;
if (num & 1 == 1) {
System.out.println("Odd");
} else {
System.out.println("Even");
}num & 1 == 1 is treated as num & (1 == 1).num & true is invalid. Correct syntax is (num & 1) == 1.isEven(int num) that returns true if num is even and false if odd, using bitwise operators. Which implementation is correct?(num & 1) == 0 returns true for even numbers.num is even. Options A, B, and D use incorrect bitwise operations that do not reliably check evenness.Pass the test and this pattern moves unknown