Bird
Raised Fist0
DSA Javaprogramming

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

Choose your learning style10 modes available
Mental Model
Every number in computers is stored in bits. The last bit tells if a number is even or odd: 0 means even, 1 means odd.
Analogy: Think of a parking lot where cars park in pairs. If there is an extra car without a pair, the number of cars is odd. The last bit is like checking if there is an extra car.
Number in bits:  ... b3 b2 b1 b0
                      ↑
                  last bit (b0)
Dry Run Walkthrough
Input: number = 5 (binary 0101)
Goal: Determine if 5 is even or odd using bit operations
Step 1: Check last bit of 5 using bitwise AND with 1
5 (0101) & 1 (0001) = 1 (0001)
Why: The last bit tells if number is odd (1) or even (0)
Step 2: Since result is 1, conclude number is odd
Result = 1 -> number is odd
Why: If last bit is 1, number is odd
Result:
5 (0101) & 1 = 1 -> number is odd
Annotated Code
DSA Java
public class EvenOddBitCheck {
    // Function to check even or odd using bits
    public static String checkEvenOdd(int num) {
        // Use bitwise AND with 1 to check last bit
        if ((num & 1) == 0) {
            return "Even"; // last bit 0 means even
        } else {
            return "Odd"; // last bit 1 means odd
        }
    }

    public static void main(String[] args) {
        int number = 5; // input number
        String result = checkEvenOdd(number);
        System.out.println(number + " is " + result);
    }
}
if ((num & 1) == 0) {
Check last bit to decide even or odd
return "Even";
Return Even if last bit is 0
return "Odd";
Return Odd if last bit is 1
OutputSuccess
5 is Odd
Complexity Analysis
Time: O(1) because bitwise operations take constant time
Space: O(1) because no extra space is used
vs Alternative: Using modulo (%) operator also takes O(1) but bitwise AND is faster and more direct
Edge Cases
number = 0
Returns Even because 0's last bit is 0
DSA Java
if ((num & 1) == 0) {
number = negative odd (e.g., -3)
Returns Odd because last bit is still 1 in two's complement
DSA Java
if ((num & 1) == 0) {
number = negative even (e.g., -4)
Returns Even because last bit is 0 in two's complement
DSA Java
if ((num & 1) == 0) {
When to Use This Pattern
When you need to quickly check if a number is even or odd without division, use bitwise AND with 1 because it directly inspects the last bit.
Common Mistakes
Mistake: Using modulo operator (%) instead of bitwise AND when bitwise is required for performance
Fix: Replace num % 2 with (num & 1) for faster check
Mistake: Checking if (num & 1) == 1 only, missing that 0 means even
Fix: Check if (num & 1) == 0 for even, else odd
Summary
It checks if a number is even or odd by looking at its last bit.
Use it when you want a fast, simple way to check even or odd without division.
The last bit of a number tells if it is even (0) or odd (1).

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