Bird
Raised Fist0
DSA C#programming~8 mins

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

Choose your learning style10 modes available
Concept Flow - Check if Number is Even or Odd Using Bits
Start with number n
↓
Perform bitwise AND: n & 1
↓
Check result
↓
End
We take the number, check its last bit using bitwise AND with 1. If result is 0, number is even; if 1, number is odd.
Execution Sample
DSA C#
int n = 5;
int result = n & 1;
if (result == 0)
    Console.WriteLine("Even");
else
    Console.WriteLine("Odd");
This code checks if 5 is even or odd by looking at its last bit.
Execution Table
StepOperationValue of nBitwise AND (n & 1)Result InterpretationOutput
1Start with n5 (binary 0101)
2Calculate n & 15 (0101)1 (0001)1 means odd
3Check if result == 051False
4Print output51OddOdd
5End51
💡 Bitwise AND result is 1, so number is odd, execution ends.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
n5555
resultundefined111
Key Moments - 3 Insights
Why do we use 'n & 1' to check even or odd?
Because the last bit of a binary number tells if it's even or odd. 'n & 1' extracts that last bit. See execution_table step 2 where n=5 (0101) & 1 (0001) gives 1, meaning odd.
What if the number is 0? Is it even or odd?
0 & 1 equals 0, so the number is even. This matches the logic in execution_table step 2 where result 0 means even.
Why not use modulo (%) operator instead of bitwise AND?
Bitwise AND is faster and directly checks the last bit. Modulo does division which is slower. The logic is the same but bitwise is more efficient, as shown in the code sample.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'result' after step 2 when n=5?
A5
B0
C1
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 4
BStep 2
CStep 1
DStep 3
💡 Hint
Look for the step where output is printed in execution_table.
If n was 4 instead of 5, what would 'n & 1' be at step 2?
A1
B0
C4
DUndefined
💡 Hint
Recall that even numbers have last bit 0, so 'n & 1' should be 0.
Concept Snapshot
Check if number is even or odd using bitwise AND:
- Use 'n & 1' to get last bit
- If result is 0, number is even
- If result is 1, number is odd
- Faster than modulo operator
- Works because binary last bit shows parity
Full Transcript
To check if a number is even or odd using bits, we look at its last binary bit. We do this by performing a bitwise AND between the number and 1. If the result is 0, the number is even; if 1, it is odd. For example, for number 5 (binary 0101), 5 & 1 equals 1, so 5 is odd. This method is efficient and simple, faster than using modulo. The code sample shows this check and prints the result accordingly.

Practice

(1/5)
1. Which bit operation can be used to check if a number is even or odd in C#?
easy
A. Using bitwise OR with 1 (number | 1)
B. Using bitwise XOR with 1 (number ^ 1)
C. Using bitwise AND with 1 (number & 1)
D. Using bitwise NOT (~number)

Solution

  1. Step 1: Understand bitwise AND with 1

    Bitwise AND with 1 checks the least significant bit of the number.
  2. Step 2: Check least significant bit for even or odd

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

    Using bitwise AND with 1 (number & 1) -> Option C
  4. Quick Check:

    Least significant bit check = number & 1 [OK]
Hint: Check last bit with & 1 to find even or odd fast [OK]
Common Mistakes:
  • Using bitwise OR instead of AND
  • Using XOR which toggles bits
  • Using NOT which flips all bits
2. Which of the following C# code snippets correctly checks if num is odd using bits?
easy
A. if ((num & 1) == 0) { Console.WriteLine("Odd"); }
B. if ((num ^ 1) == 1) { Console.WriteLine("Odd"); }
C. if ((num | 1) == 1) { Console.WriteLine("Odd"); }
D. if ((num & 1) == 1) { Console.WriteLine("Odd"); }

Solution

  1. Step 1: Understand bitwise AND check for odd

    Odd numbers have least significant bit 1, so (num & 1) == 1 means odd.
  2. Step 2: Verify code logic

    if ((num & 1) == 1) { Console.WriteLine("Odd"); } prints "Odd" when (num & 1) equals 1, correctly identifying odd numbers.
  3. Final Answer:

    if ((num & 1) == 1) { Console.WriteLine("Odd"); } -> Option D
  4. Quick Check:

    Odd check = (num & 1) == 1 [OK]
Hint: Use (num & 1) == 1 to check odd numbers [OK]
Common Mistakes:
  • Checking (num & 1) == 0 for odd (actually even)
  • Using OR or XOR instead of AND
  • Confusing equality check with assignment
3. What is the output of the following C# code?
int num = 14;
if ((num & 1) == 0)
    Console.WriteLine("Even");
else
    Console.WriteLine("Odd");
medium
A. Odd
B. Even
C. 14
D. Error

Solution

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

    14 in binary is 1110; 1110 & 0001 = 0, so (num & 1) == 0 is true.
  2. Step 2: Determine output based on condition

    Since condition is true, it prints "Even".
  3. Final Answer:

    Even -> Option B
  4. Quick Check:

    14 & 1 = 0 means Even [OK]
Hint: Even numbers have last bit 0, so (num & 1) == 0 [OK]
Common Mistakes:
  • Assuming 14 is odd
  • Confusing bitwise AND with OR
  • Expecting numeric output instead of string
4. Find the error in this C# code that checks if a number is odd:
int num = 7;
if (num & 1 == 1)
    Console.WriteLine("Odd");
else
    Console.WriteLine("Even");
medium
A. Missing parentheses around (num & 1)
B. Using bitwise OR instead of AND
C. Incorrect variable type for num
D. No error, code is correct

Solution

  1. Step 1: Analyze operator precedence

    In C#, equality (==) has higher precedence than bitwise AND (&), so 'num & 1 == 1' parses as 'num & (1 == 1)', which is 'num & 1'. This does not compare (num & 1) to 1 as intended.
  2. Step 2: Identify the error

    Missing parentheses around (num & 1) causes incorrect evaluation. Correct code should be 'if ((num & 1) == 1)'.
  3. Final Answer:

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

    Use parentheses to ensure correct precedence [OK]
Hint: C# precedence: == > & , so use parentheses to ensure correct evaluation [OK]
Common Mistakes:
  • Misjudging precedence (thinking & > ==)
  • Using bitwise OR instead of AND
  • Assuming parentheses are not required
5. You want to write a C# method that returns "Even" or "Odd" for any integer using bitwise operations. Which of these implementations is correct and efficient?
public string CheckEvenOdd(int num) {
    // Options:
    // A) return (num & 1) == 0 ? "Even" : "Odd";
    // B) return (num | 1) == 0 ? "Even" : "Odd";
    // C) return (num ^ 1) == 0 ? "Even" : "Odd";
    // D) return (~num & 1) == 0 ? "Even" : "Odd";
}
hard
A. Option A
B. Option B
C. Option C
D. Option D

Solution

  1. Step 1: Evaluate each option's logic

    Option A correctly checks if least significant bit is 0 for even; others use incorrect bitwise logic.
  2. Step 2: Confirm correctness and efficiency

    Option A uses simple bitwise AND and ternary operator, efficient and clear.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Use (num & 1) == 0 for even check [OK]
Hint: Use (num & 1) == 0 ? "Even" : "Odd" for quick check [OK]
Common Mistakes:
  • Using OR or XOR which give wrong results
  • Using NOT which flips bits incorrectly
  • Ignoring ternary operator for return
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