Bird
Raised Fist0
DSA C++programming~20 mins

Check if Number is Even or Odd Using Bits in DSA C++ - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bitwise Even-Odd Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this C++ code snippet?
Consider the following code that checks if a number is even or odd using bitwise operations. What will it print?
DSA C++
#include <iostream>
int main() {
    int num = 7;
    if (num & 1) {
        std::cout << "Odd" << std::endl;
    } else {
        std::cout << "Even" << std::endl;
    }
    return 0;
}
ACompilation error
B"Odd"
C"Even"
DRuntime error
Attempts:
2 left
💡 Hint
The bitwise AND with 1 checks the least significant bit of the number.
❓ Predict Output
intermediate
2:00remaining
What is the output when num = 12?
Using the same logic as before, what will the program print if num is 12?
DSA C++
#include <iostream>
int main() {
    int num = 12;
    if (num & 1) {
        std::cout << "Odd" << std::endl;
    } else {
        std::cout << "Even" << std::endl;
    }
    return 0;
}
A"Odd"
BCompilation error
C"Even"
DRuntime error
Attempts:
2 left
💡 Hint
Check the least significant bit of 12 in binary.
🧠 Conceptual
advanced
2:00remaining
Why does using bitwise AND with 1 check if a number is even or odd?
Select the best explanation for why the expression (num & 1) can determine if num is even or odd.
ABecause it converts the number to its negative equivalent.
BBecause bitwise AND with 1 shifts the number right by one bit.
CBecause it counts the total number of bits set to 1 in the number.
DBecause the least significant bit of an odd number is always 1, and for even numbers it is 0.
Attempts:
2 left
💡 Hint
Think about binary representation of even and odd numbers.
🔧 Debug
advanced
2:00remaining
Does this code work correctly?
Examine this code snippet that attempts to check if a number is even or odd using bits. Select the correct statement.
DSA C++
#include <iostream>
int main() {
    int num = 5;
    if (num & 1 == 1) {
        std::cout << "Odd" << std::endl;
    } else {
        std::cout << "Even" << std::endl;
    }
    return 0;
}
AWorks correctly, no error
BCompilation error due to operator precedence
CRuntime error due to invalid operation
DAlways prints "Even" regardless of num
Attempts:
2 left
💡 Hint
Check operator precedence between & and == operators. Which has higher precedence?
❓ Predict Output
expert
2:00remaining
What is the output of this code with negative number?
What will this program print when num = -3?
DSA C++
#include <iostream>
int main() {
    int num = -3;
    if (num & 1) {
        std::cout << "Odd" << std::endl;
    } else {
        std::cout << "Even" << std::endl;
    }
    return 0;
}
A"Odd"
BRuntime error
CCompilation error
D"Even"
Attempts:
2 left
💡 Hint
Consider how negative numbers are represented in two's complement.

Practice

(1/5)
1. Which bit operation can be used to check if a number is even or odd?
easy
A. Check the least significant bit using AND with 1
B. Shift the number right by 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. If LSB is 0, number is even; if 1, number is odd.
  2. Step 2: Use AND operation with 1 to check LSB

    Performing bitwise AND with 1 isolates the LSB. Result 0 means even, 1 means odd.
  3. Final Answer:

    Check the least significant bit using AND with 1 -> Option A
  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 num is odd using bits?
easy
A. if ((num & 1) == 0) { /* odd */ }
B. if ((num >> 1) == 1) { /* odd */ }
C. if ((num ^ 1) == 1) { /* odd */ }
D. if ((num & 1) == 1) { /* odd */ }

Solution

  1. Step 1: Understand bitwise AND with 1

    Using (num & 1) extracts the least significant bit. If it equals 1, number is odd.
  2. Step 2: Check condition for odd number

    The condition (num & 1) == 1 correctly identifies odd numbers.
  3. Final Answer:

    if ((num & 1) == 1) { /* odd */ } -> Option D
  4. Quick Check:

    (num & 1) == 1 means odd [OK]
Hint: Use (num & 1) == 1 to check odd numbers [OK]
Common Mistakes:
  • Using == 0 to check odd (actually checks even)
  • Using bit shift without comparison
  • Using XOR incorrectly for odd check
3. What is the output of the following C++ code?
int num = 14;
if (num & 1) {
    std::cout << "Odd";
} else {
    std::cout << "Even";
}
medium
A. Odd
B. Even
C. 14
D. Error

Solution

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

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

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

    Even -> Option B
  4. Quick Check:

    14 & 1 = 0 means 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 arithmetic AND
  • Printing num instead of "Even" or "Odd"
4. Identify the error in the following code snippet that checks if a number is odd:
int num = 7;
if (num & 1 == 1) {
    std::cout << "Odd";
} else {
    std::cout << "Even";
}
medium
A. Incorrect variable type for num
B. Using == instead of =
C. Missing parentheses around (num & 1)
D. No error, code is correct

Solution

  1. Step 1: Analyze operator precedence in condition

    In C++, == has higher precedence than &. So, num & 1 == 1 is parsed as num & (1 == 1), which is num & 1. This is not the intended comparison.
  2. Step 2: Identify the error

    Without parentheses, the expression does not explicitly compare (num & 1) to 1, which can cause confusion or unintended behavior. Parentheses are needed: (num & 1) == 1.
  3. Final Answer:

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

    Operator precedence requires parentheses for clarity [OK]
Hint: Use parentheses: (num & 1) == 1 to avoid precedence issues [OK]
Common Mistakes:
  • Misinterpreting operator precedence (== > &)
  • Thinking parentheses are not needed
  • Believing the code works correctly without parentheses
5. You want to write a function in C++ that returns true if a number is even and false if odd, using bitwise operations. Which of the following implementations is correct?
hard
A. bool isEven(int n) { return (n & 1) == 0; }
B. bool isEven(int n) { return (n & 1) == 1; }
C. bool isEven(int n) { return (n >> 1) == 0; }
D. bool isEven(int n) { return !(n ^ 1); }

Solution

  1. Step 1: Understand even number bit pattern

    Even numbers have least significant bit 0. So (n & 1) == 0 means even.
  2. Step 2: Check each function implementation

    bool isEven(int n) { return (n & 1) == 1; } returns true for odd numbers, so incorrect. bool isEven(int n) { return (n & 1) == 0; } correctly returns true for even numbers. Options C and D do not correctly check evenness.
  3. Final Answer:

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

    Even if LSB is 0 [OK]
Hint: Return (n & 1) == 0 for even check [OK]
Common Mistakes:
  • Returning true for odd numbers by mistake
  • Using bit shift incorrectly for even check
  • Using XOR without understanding its effect
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