Challenge - 5 Problems
Bitwise Even-Odd Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this C# code using bitwise AND to check even or odd?
Look at the code below. What will it print when number = 7?
DSA C#
int number = 7; string result = (number & 1) == 0 ? "Even" : "Odd"; Console.WriteLine(result);
Attempts:
2 left
💡 Hint
The bitwise AND with 1 checks the last bit of the number.
✗ Incorrect
The expression (number & 1) checks if the last bit is 1 (odd) or 0 (even). Since 7 in binary ends with 1, it is odd.
❓ Predict Output
intermediateWhat is the output when number = 12 in this bitwise even/odd check?
Check the output of this code snippet:
DSA C#
int number = 12; string result = (number & 1) == 0 ? "Even" : "Odd"; Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Even numbers have last bit 0 in binary.
✗ Incorrect
12 in binary ends with 0, so (12 & 1) == 0 is true, printing "Even".
🔧 Debug
advancedWhat error occurs in this C# code checking even/odd with bits?
Identify the error when running this code:
DSA C#
int number = 5; string result = (number & 1) == 0 ? "Even" : "Odd"; Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Check punctuation carefully in C# statements.
✗ Incorrect
The first line is missing a semicolon at the end, causing a syntax error.
❓ Predict Output
advancedWhat is the output of this code that uses bitwise XOR to check even or odd?
What does this print when number = 10?
DSA C#
int number = 10; string result = ((number ^ 1) == number + 1) ? "Odd" : "Even"; Console.WriteLine(result);
Attempts:
2 left
💡 Hint
XOR with 1 flips the last bit.
✗ Incorrect
For even 10, 10 ^ 1 = 11 == 10 + 1, so condition true, prints "Odd".
❓ Predict Output
expertWhat is the output of this C# code that uses bitwise operations to count even numbers in an array?
Given the array, how many even numbers does this code count?
DSA C#
int[] numbers = {3, 4, 7, 8, 10}; int count = 0; foreach (int num in numbers) { if ((num & 1) == 0) { count++; } } Console.WriteLine(count);
Attempts:
2 left
💡 Hint
Count numbers whose last bit is 0.
✗ Incorrect
4, 8, and 10 are even, so count is 3.
