📖 Scenario: Imagine you are building a simple program that helps a cashier quickly check if a number is even or odd using a fast method. This can be useful in many places where quick decisions are needed based on numbers.
🎯 Goal: You will create a program that uses bit operations to check if a number is even or odd and then prints the result.
📋 What You'll Learn
Create an integer variable named number with the value 29
Create an integer variable named bit_mask with the value 1
Use a bitwise AND operation between number and bit_mask to check if the number is even or odd
Print "Odd" if the number is odd, otherwise print "Even"
💡 Why This Matters
🌍 Real World
Bitwise operations are used in low-level programming, embedded systems, and performance-critical applications to quickly check or manipulate data.
💼 Career
Understanding bitwise operations helps in roles like embedded software development, systems programming, and optimizing algorithms.
Progress0 / 4 steps
1
Create the number variable
Create an integer variable called number and set it to 29.
DSA C++
Hint
Use int number = 29; to create the variable.
2
Create the bit mask variable
Create an integer variable called bit_mask and set it to 1.
DSA C++
Hint
Use int bit_mask = 1; to create the mask for checking the last bit.
3
Check if the number is even or odd using bitwise AND
Use an if statement to check if number & bit_mask is equal to 1. If yes, the number is odd; otherwise, it is even.
DSA C++
Hint
Use if ((number & bit_mask) == 1) to check the last bit.
4
Print the result
Inside the if block, print "Odd". Inside the else block, print "Even".
DSA C++
Hint
Use std::cout << "Odd" << std::endl; and std::cout << "Even" << std::endl; to print the result.
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.