Bird
Raised Fist0
DSA C#programming

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

Choose your learning style10 modes available
Mental Model
The last bit of a number in binary tells if it is even or odd. If the last bit is 0, the number is even; if 1, it is odd.
Analogy: Think of a parking lot where cars park in spots numbered 0, 1, 2, 3, and so on. If the spot number ends with a 0 in binary, it's like an even spot; if it ends with 1, it's an odd spot.
Number in binary:  ... b3 b2 b1 b0
                      ----------------
                      ...  0  1  0  1  (last bit b0 is 1 means odd)
                      ...  1  0  0  0  (last bit b0 is 0 means even)
Dry Run Walkthrough
Input: number = 5
Goal: Determine if 5 is even or odd using bit operations
Step 1: Perform bitwise AND between number and 1 (5 & 1)
5 in binary: 0101
1 in binary:  0001
Result:       0001 (which is 1)
Why: AND with 1 isolates the last bit to check if it is 0 or 1
Step 2: Check if result is 0 or 1
Result is 1
Why: If last bit is 1, number is odd
Result:
5 is odd
Annotated Code
DSA C#
using System;

class Program
{
    static bool IsEven(int number)
    {
        // Check last bit using bitwise AND
        return (number & 1) == 0;
    }

    static void Main()
    {
        int number = 5;
        if (IsEven(number))
            Console.WriteLine($"{number} is even");
        else
            Console.WriteLine($"{number} is odd");
    }
}
return (number & 1) == 0;
Check last bit to determine even or odd
if (IsEven(number))
Decide output based on bit check
OutputSuccess
5 is odd
Complexity Analysis
Time: O(1) because bitwise operations take constant time regardless of number size
Space: O(1) because no extra memory is used besides input and a few variables
vs Alternative: Using modulo (%) operator also takes O(1) time but bitwise AND is faster and more efficient at low level
Edge Cases
number = 0
Returns even because 0 & 1 == 0
DSA C#
return (number & 1) == 0;
number = -2
Returns even because bitwise AND checks last bit regardless of sign
DSA C#
return (number & 1) == 0;
number = 1
Returns odd because 1 & 1 == 1
DSA C#
return (number & 1) == 0;
When to Use This Pattern
When you need a fast way to check even or odd without division, use bitwise AND with 1 because it directly inspects the last binary digit.
Common Mistakes
Mistake: Using modulo operator (%) instead of bitwise AND for this specific check
Fix: Replace '%' with '&' to use bitwise operation for better performance
Mistake: Checking if (number & 1) == 1 and returning even instead of odd
Fix: Remember (number & 1) == 0 means even, == 1 means odd
Summary
It checks if a number is even or odd by looking at its last binary bit.
Use it when you want a quick, efficient way to test evenness without division.
The last bit of a binary number tells you if it is even (0) or odd (1).

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