Bird
Raised Fist0
DSA Javaprogramming~10 mins

Check if Number is Even or Odd Using Bits in DSA Java - Execution Trace

Choose your learning style10 modes available
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.
Execution Sample
DSA Java
int n = 5;
if ((n & 1) == 0) {
    System.out.println("Even");
} else {
    System.out.println("Odd");
}
This code uses bitwise AND with 1 to check if the number is even or odd.
Execution Table
StepOperationNumber (n)Bitwise AND (n & 1)Condition (== 0)?Result
1Start5 (binary 0101)
2Calculate n & 151 (binary 0001)
3Check if (n & 1) == 051FalseOdd
4Print result5Odd
5End5Stop
💡 Bitwise AND result is 1, so condition is false; number is Odd, execution stops.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
n5555
n & 1111
Condition (n & 1 == 0)FalseFalse
ResultOdd
Key Moments - 3 Insights
Why do we use (n & 1) to check even or odd?
Because the least significant bit (LSB) of a binary number tells if it is even (0) or odd (1). See execution_table step 2 where n & 1 extracts the LSB.
What does it mean if (n & 1) equals 0?
It means the number's last bit is 0, so the number is even. Refer to execution_table step 3 where condition is true for even numbers.
Why not use modulo (%) operator instead?
Bitwise AND is faster and directly checks the last bit without division. This is efficient for low-level checks as shown in the code sample.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of (n & 1) at step 2 for n=5?
A0
B1
C5
DUndefined
💡 Hint
Check the 'Bitwise AND (n & 1)' column at step 2 in execution_table.
At which step does the program decide the number is odd?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Condition (== 0)?' and 'Result' columns in execution_table.
If n was 8, what would (n & 1) be at step 2?
A8
B1
C0
DCannot determine
💡 Hint
Recall that even numbers have last bit 0; check how (n & 1) extracts last bit.
Concept Snapshot
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
Full Transcript
To check if a number is even or odd using bits, we look at its least significant bit (LSB). The code uses bitwise AND with 1 (n & 1) to extract this bit. If the result is 0, the number is even; if 1, it is odd. For example, for n=5 (binary 0101), n & 1 equals 1, so it is odd. This method is efficient and faster than using modulo. The execution table shows each step: starting with n=5, calculating n & 1, checking the condition, printing the result, and stopping. Variables like n, n & 1, and the condition are tracked through the steps. Common confusions include why bitwise AND works for this check and why it is preferred over modulo. The visual quiz tests understanding of these steps and values. This approach is a quick way to determine parity using binary operations.

Practice

(1/5)
1. What does the expression num & 1 check when determining if a number is even or odd?
easy
A. It checks if the least significant bit of num is 1, indicating odd number
B. It checks if num is greater than 1
C. It shifts num one bit to the right
D. It adds 1 to num

Solution

  1. Step 1: Understand bitwise AND operation

    The expression num & 1 performs a bitwise AND between num and 1, which isolates the least significant bit (rightmost bit) of num.
  2. Step 2: Interpret the least significant bit

    If the least significant bit is 1, the number is odd; if it is 0, the number is even.
  3. Final Answer:

    It checks if the least significant bit of num is 1, indicating odd number -> Option A
  4. Quick Check:

    num & 1 = 1 means odd [OK]
Hint: Check last bit with & 1; 1 means odd, 0 means even [OK]
Common Mistakes:
  • Thinking & 1 shifts bits instead of checking last bit
  • Confusing bitwise AND with addition
  • Assuming & 1 checks if number is greater than 1
2. Which of the following Java code snippets correctly checks if an integer num is even using bitwise operators?
easy
A. if ((num & 0) == 1) { System.out.println("Even"); }
B. if ((num | 1) == 0) { System.out.println("Even"); }
C. if ((num & 1) == 0) { System.out.println("Even"); }
D. if ((num ^ 1) == 0) { System.out.println("Even"); }

Solution

  1. Step 1: Analyze bitwise AND with 1

    The expression num & 1 isolates the least significant bit. If it equals 0, the number is even.
  2. Step 2: Check correctness of conditions

    if ((num & 1) == 0) { System.out.println("Even"); } correctly checks if (num & 1) == 0 to print "Even". Other options misuse bitwise OR, AND with 0, or XOR, which do not correctly check evenness.
  3. Final Answer:

    if ((num & 1) == 0) { System.out.println("Even"); } -> Option C
  4. Quick Check:

    num & 1 == 0 means even [OK]
Hint: Use (num & 1) == 0 to check even numbers [OK]
Common Mistakes:
  • Using bitwise OR instead of AND
  • Comparing with wrong values like 1 when checking even
  • Using XOR incorrectly for even check
3. What is the output of the following Java code?
int num = 7;
if ((num & 1) == 0) {
    System.out.println("Even");
} else {
    System.out.println("Odd");
}
medium
A. Compilation error
B. Even
C. 0
D. Odd

Solution

  1. Step 1: Calculate bitwise AND of num and 1

    7 in binary is 0111. 0111 & 0001 = 0001, which equals 1.
  2. Step 2: Check condition and print output

    Since (num & 1) == 0 is false, the else block executes, printing "Odd".
  3. Final Answer:

    Odd -> Option D
  4. Quick Check:

    7 & 1 = 1 means odd [OK]
Hint: If num & 1 equals 1, print Odd; else Even [OK]
Common Mistakes:
  • Assuming 7 is even
  • Confusing bitwise AND with arithmetic AND
  • Expecting output 0 or error
4. Identify the error in the following Java code that tries to check if a number is odd using bits:
int num = 10;
if (num & 1 == 1) {
    System.out.println("Odd");
} else {
    System.out.println("Even");
}
medium
A. No error, code is correct
B. Missing parentheses around (num & 1)
C. Incorrect variable type for num
D. Using bitwise OR instead of AND

Solution

  1. Step 1: Analyze operator precedence in condition

    In Java, == has higher precedence than &. So num & 1 == 1 is treated as num & (1 == 1).
  2. Step 2: Fix condition with parentheses

    Since (1 == 1) is true (boolean), num & true is invalid. Correct syntax is (num & 1) == 1.
  3. Final Answer:

    Missing parentheses around (num & 1) -> Option B
  4. Quick Check:

    Use parentheses to ensure correct bitwise operation [OK]
Hint: Always parenthesize bitwise ops before comparison [OK]
Common Mistakes:
  • Ignoring operator precedence causing syntax errors
  • Using bitwise OR instead of AND
  • Assuming code compiles without parentheses
5. You want to write a Java method isEven(int num) that returns true if num is even and false if odd, using bitwise operators. Which implementation is correct?
hard
A. public boolean isEven(int num) { return (num & 1) == 0; }
B. public boolean isEven(int num) { return (num | 1) == 0; }
C. public boolean isEven(int num) { return (num ^ 1) == 0; }
D. public boolean isEven(int num) { return (num & 0) == 1; }

Solution

  1. Step 1: Understand bitwise check for even

    Even numbers have least significant bit 0, so (num & 1) == 0 returns true for even numbers.
  2. Step 2: Evaluate each option

    public boolean isEven(int num) { return (num & 1) == 0; } correctly returns true if num is even. Options A, B, and D use incorrect bitwise operations that do not reliably check evenness.
  3. Final Answer:

    public boolean isEven(int num) { return (num & 1) == 0; } -> Option A
  4. Quick Check:

    Use (num & 1) == 0 to return true for even [OK]
Hint: Return (num & 1) == 0 for even check in method [OK]
Common Mistakes:
  • Using bitwise OR or XOR incorrectly
  • Comparing with wrong values
  • Returning wrong boolean for even check
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard