What bit operation can be used to check if a number is even or odd?
The bitwise AND operation with 1 (number & 1) can be used. If the result is 0, the number is even; if 1, the number is odd.
Click to reveal answer
beginner
Why does checking the least significant bit (LSB) tell us if a number is even or odd?
Because in binary, even numbers always end with 0 and odd numbers end with 1 in their least significant bit.
Click to reveal answer
beginner
What is the output of the expression (5 & 1)?
The output is 1 because 5 in binary is 0101 and 1 is 0001; AND operation results in 0001 which is 1, indicating 5 is odd.
Click to reveal answer
beginner
Write a simple C++ code snippet to check if a number is even using bitwise operators.
int num = 4;
if ((num & 1) == 0) {
// number is even
} else {
// number is odd
}
Click to reveal answer
intermediate
Is using bitwise AND faster than using modulo (%) to check even or odd? Why?
Yes, bitwise AND is generally faster because it directly checks the least significant bit without performing division, which is more computationally expensive.
Click to reveal answer
Which bitwise operation helps to check if a number is odd or even?
AOR with 1
BAND with 1
CXOR with 1
DNOT operation
✗ Incorrect
AND with 1 checks the least significant bit to determine if a number is odd (1) or even (0).
What does the expression (num & 1) == 0 mean?
Anum is zero
Bnum is odd
Cnum is negative
Dnum is even
✗ Incorrect
If the least significant bit is 0, the number is even.
What is the binary representation of the number 6?
A1001
B0101
C0110
D1110
✗ Incorrect
6 in binary is 0110.
If (num & 1) results in 1, what can we say about num?
Anum is odd
Bnum is even
Cnum is zero
Dnum is negative
✗ Incorrect
A result of 1 means the least significant bit is set, so num is odd.
Which is generally faster for checking even or odd?
ABitwise AND (&)
BAddition operator (+)
CDivision operator (/)
DModulo operator (%)
✗ Incorrect
Bitwise AND is faster because it directly checks the least significant bit without division.
Explain how to check if a number is even or odd using bitwise operations.
Think about the last bit in binary numbers.
You got /4 concepts.
Write a simple C++ code snippet that uses bitwise operators to print if a number is even or odd.
Use (num & 1) to check the number.
You got /3 concepts.
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
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.
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.
Final Answer:
Check the least significant bit using AND with 1 -> Option A
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
Step 1: Understand bitwise AND with 1
Using (num & 1) extracts the least significant bit. If it equals 1, number is odd.
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
Step 1: Evaluate bitwise AND of num and 1
14 in binary is 1110. The least significant bit is 0. So, 14 & 1 = 0.
Step 2: Check the if condition
Since (num & 1) is 0, the else block executes, printing "Even".
Final Answer:
Even -> Option B
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
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.
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.
Final Answer:
Missing parentheses around (num & 1) -> Option C
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
Step 1: Understand even number bit pattern
Even numbers have least significant bit 0. So (n & 1) == 0 means even.
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.