Bird
Raised Fist0
DSA Cprogramming~5 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
↓
Check least significant bit (n & 1)
↓
Even
↓
Output Even
↓
End
Check the last bit of the number using bitwise AND with 1; if 0, number is even, else odd.
Execution Sample
DSA C
int n = 5;
if (n & 1) {
  printf("Odd\n");
} else {
  printf("Even\n");
}
This code checks if the number 5 is even or odd by looking at its last bit.
Execution Table
StepOperationNumber (n)Bitwise AND (n & 1)ResultOutput
1Start with number5
2Calculate n & 151True (1)
3Check if (n & 1) == 151OddPrint "Odd"
4End5Output complete
💡 Bitwise AND result is 1, so number is odd; execution ends after printing.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
n5555
n & 1111
OutputOddOdd
Key Moments - 3 Insights
Why do we use 'n & 1' to check even or odd?
Because the least significant bit (rightmost bit) of an even number is 0, and for odd number is 1. See execution_table step 2 where n & 1 is 1 for number 5.
What if the number is 0? Is it even or odd?
0 & 1 equals 0, so it is even. The check works the same way as shown in execution_table step 2 logic.
Why not use modulo (%) operator instead of bitwise AND?
Bitwise AND is faster and directly checks the last bit, which is enough to determine even or odd. This is shown in the execution_table where only one bit check is done.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'n & 1' at step 2 for number 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 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Result' and 'Output' columns in execution_table to find when odd is printed.
If the number 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; check variable_tracker for 'n & 1' values.
Concept Snapshot
Check if number is even or odd using bitwise AND:
- Use expression (n & 1)
- If result is 0, number is even
- If result is 1, number is odd
- This checks only the last bit
- Faster than modulo (%) operator
Full Transcript
To check if a number is even or odd using bits, we look at its least significant bit. We do this by performing a bitwise AND between the number and 1 (n & 1). If the result is 0, the number is even because its last bit is 0. If the result is 1, the number is odd because its last bit is 1. For example, number 5 in binary ends with 1, so 5 & 1 equals 1, meaning odd. This method is efficient and simple, faster than using modulo. The code prints 'Odd' or 'Even' based on this check. This approach works for all integers including zero, which is even.

Practice

(1/5)
1. Which bit operation can be used to check if a number is even or odd?
easy
A. Shift the number right by 1
B. Check the least significant bit using AND with 1
C. XOR the number with 1
D. Invert all bits of the number

Solution

  1. Step 1: Understand the least significant bit (LSB)

    The LSB of a binary number determines if it is even or odd: 0 means even, 1 means odd.
  2. Step 2: Use AND operation with 1 to isolate LSB

    Performing bitwise AND with 1 keeps only the LSB. If result is 0, number is even; if 1, odd.
  3. Final Answer:

    Check the least significant bit using AND with 1 -> Option B
  4. Quick Check:

    LSB & 1 = 0 (even) or 1 (odd) [OK]
Hint: Use n & 1 to check even or odd quickly [OK]
Common Mistakes:
  • Using XOR instead of AND to check LSB
  • Checking the most significant bit instead
  • Using bit shift without checking bits
2. Which of the following C code snippets correctly checks if an integer n is odd using bitwise operators?
easy
A. if ((n & 1) == 1) printf("Odd");
B. if ((n >> 1) == 1) printf("Odd");
C. if ((n & 1) == 0) printf("Odd");
D. if ((n ^ 1) == 1) printf("Odd");

Solution

  1. Step 1: Understand bitwise AND with 1

    Using n & 1 isolates the least significant bit. If it equals 1, the number is odd.
  2. Step 2: Check condition for odd number

    The correct condition is (n & 1) == 1 to print "Odd".
  3. Final Answer:

    if ((n & 1) == 1) printf("Odd"); -> Option A
  4. Quick Check:

    n & 1 == 1 means odd [OK]
Hint: Use (n & 1) == 1 to detect odd numbers [OK]
Common Mistakes:
  • Checking for == 0 to print odd (actually even)
  • Using right shift instead of AND
  • Using XOR incorrectly for odd check
3. What is the output of the following C code?
int n = 14;
if (n & 1)
    printf("Odd");
else
    printf("Even");
medium
A. Odd
B. Compilation error
C. Even
D. 14

Solution

  1. Step 1: Evaluate n & 1 for n = 14

    Binary of 14 is 1110. The least significant bit is 0. So, 14 & 1 = 0.
  2. Step 2: Check the if condition

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

    Even -> Option C
  4. Quick Check:

    14 is even, so output is Even [OK]
Hint: Check LSB: 0 means even, 1 means odd [OK]
Common Mistakes:
  • Assuming 14 is odd because it ends with 4
  • Confusing bitwise AND with logical AND
  • Expecting output "14" instead of even/odd
4. Identify the error in this C code snippet that checks if a number is odd:
int n = 7;
if (n & 1 == 1)
    printf("Odd");
else
    printf("Even");
medium
A. Missing parentheses around (n & 1)
B. Using == instead of = in if condition
C. Incorrect use of bitwise OR instead of AND
D. No error, code works fine

Solution

  1. Step 1: Analyze operator precedence in condition

    In C, == has higher precedence than &. So expression is evaluated as n & (1 == 1).
  2. Step 2: Evaluate the expression and fix

    Since (1 == 1) is true (1), expression becomes n & 1, which is correct but confusing. To avoid this, parentheses should be used: (n & 1) == 1.
  3. Final Answer:

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

    Use (n & 1) == 1 to ensure correct evaluation [OK]
Hint: Use parentheses: (n & 1) == 1 to avoid precedence errors [OK]
Common Mistakes:
  • Ignoring operator precedence causing wrong evaluation
  • Using assignment = instead of comparison ==
  • Confusing bitwise AND with logical AND
5. You want to write a function in C that returns 1 if a number is odd and 0 if even, using bitwise operators. Which of the following implementations is correct and efficient?
int isOdd(int n) {
    // Choose one
}
hard
A. return (n >> 1) & 1;
B. return !(n & 1);
C. return n ^ 1;
D. return (n & 1) == 1 ? 1 : 0;

Solution

  1. Step 1: Understand the goal of the function

    The function should return 1 if n is odd, 0 if even, using bitwise operations.
  2. Step 2: Evaluate each option

    return (n & 1) == 1 ? 1 : 0; correctly returns 1 if LSB is 1 (odd), else 0. return (n >> 1) & 1; returns the second least significant bit, incorrect. return n ^ 1; returns n XOR 1, which changes the number, not a boolean. return !(n & 1); returns 1 if even, opposite of requirement.
  3. Final Answer:

    return (n & 1) == 1 ? 1 : 0; -> Option D
  4. Quick Check:

    Use (n & 1) == 1 to detect odd and return 1 [OK]
Hint: Return (n & 1) == 1 for odd check function [OK]
Common Mistakes:
  • Using bit shift instead of checking LSB
  • Returning modified number instead of boolean
  • Negating the condition incorrectly
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