What is the output of the bitwise operation 6 & 1?
6 in binary is 110, 1 is 001. 110 & 001 = 000, which is 0, so 6 is even.
Click to reveal answer
intermediate
Can this bitwise method be used for negative numbers to check even or odd? Why or why not?
Yes, because the least significant bit still indicates even or odd regardless of sign in two's complement representation.
Click to reveal answer
Which bitwise operator is used to check if a number is even or odd?
A^ (XOR)
B& (AND)
C| (OR)
D~ (NOT)
✗ Incorrect
The AND operator with 1 isolates the least significant bit to check even or odd.
What does (num & 1) == 0 mean for an integer num?
Anum is even
Bnum is odd
Cnum is negative
Dnum is zero
✗ Incorrect
If the least significant bit is 0, the number is even.
What is the result of 7 & 1 in binary?
A7
B0
C1
D6
✗ Incorrect
7 in binary is 111, 111 & 001 = 001 which is 1, so 7 is odd.
Why is bitwise checking faster than using modulo (%) for even/odd check?
AModulo is not supported in hardware
BModulo always returns zero
CBitwise uses less memory
DBitwise operations are simpler and faster at CPU level
✗ Incorrect
Bitwise operations are low-level and faster than modulo arithmetic.
Is the expression (num & 1) == 1 true for even numbers?
ANo
BOnly for negative numbers
COnly for zero
DYes
✗ Incorrect
It is true only for odd numbers, not even.
Explain how you can use bitwise operations to check if a number is even or odd.
Think about the last binary digit of the number.
You got /4 concepts.
Write a simple C# code snippet that prints 'Even' or 'Odd' based on bitwise check.
Use the AND operator and compare with 1.
You got /4 concepts.
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
Step 1: Understand bitwise AND with 1
Bitwise AND with 1 checks the least significant bit of the number.
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.
Final Answer:
Using bitwise AND with 1 (number & 1) -> Option C
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
Step 1: Understand bitwise AND check for odd
Odd numbers have least significant bit 1, so (num & 1) == 1 means odd.
Step 2: Verify code logic
if ((num & 1) == 1) { Console.WriteLine("Odd"); } prints "Odd" when (num & 1) equals 1, correctly identifying odd numbers.
Final Answer:
if ((num & 1) == 1) { Console.WriteLine("Odd"); } -> Option D
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
Step 1: Calculate bitwise AND of 14 and 1
14 in binary is 1110; 1110 & 0001 = 0, so (num & 1) == 0 is true.
Step 2: Determine output based on condition
Since condition is true, it prints "Even".
Final Answer:
Even -> Option B
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
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.
Step 2: Identify the error
Missing parentheses around (num & 1) causes incorrect evaluation. Correct code should be 'if ((num & 1) == 1)'.
Final Answer:
Missing parentheses around (num & 1) -> Option A
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?