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 Go code checking even or odd using bits?
Look at the code below. What will it print when run?
DSA Go
package main import "fmt" func main() { num := 7 if num&1 == 0 { fmt.Println("Even") } else { fmt.Println("Odd") } }
Attempts:
2 left
💡 Hint
Check the last bit of the number using bitwise AND with 1.
✗ Incorrect
The bitwise AND of 7 (binary 111) and 1 (binary 001) is 1, which is not zero, so the number is odd.
❓ Predict Output
intermediateWhat does this Go code print for number 12 using bitwise check?
Check the output of this code snippet:
DSA Go
package main import "fmt" func main() { num := 12 if num&1 == 0 { fmt.Println("Even") } else { fmt.Println("Odd") } }
Attempts:
2 left
💡 Hint
Even numbers have last bit 0 in binary.
✗ Incorrect
12 in binary ends with 0, so 12 & 1 equals 0, meaning the number is even.
🔧 Debug
advancedWhat error does this Go code produce when checking even or odd?
Find the error in this code snippet:
DSA Go
package main import "fmt" func main() { num := 5 if num&1 == 0 { fmt.Println("Even") } else { fmt.Println("Odd") } }
Attempts:
2 left
💡 Hint
Check the operator used in the if condition.
✗ Incorrect
The code uses assignment '=' instead of comparison '==' in the if condition, causing a syntax error.
🧠 Conceptual
advancedWhy does using bitwise AND with 1 check if a number is even or odd?
Choose the best explanation:
Attempts:
2 left
💡 Hint
Think about binary representation of even and odd numbers.
✗ Incorrect
Odd numbers have their least significant bit set to 1, so num & 1 equals 1 for odd numbers.
🚀 Application
expertWhat is the output of this Go program that counts even numbers using bitwise check?
Analyze the code and select the correct output:
DSA Go
package main import "fmt" func main() { nums := []int{2, 3, 4, 5, 6} count := 0 for _, n := range nums { if n&1 == 0 { count++ } } fmt.Println(count) }
Attempts:
2 left
💡 Hint
Count how many numbers in the list have last bit 0.
✗ Incorrect
Numbers 2, 4, and 6 are even, so count is 3.
