What is the output of checking number 10 using bitwise AND with 1?
10 in binary is 1010. 1010 & 0001 = 0, so 10 is even.
Click to reveal answer
intermediate
Is using bitwise operation to check even or odd faster than using modulo (%) operator?
Yes, bitwise operations are generally faster because they work directly on bits without division.
Click to reveal answer
Which bitwise operation helps to check if a number is odd or even?
AAND with 1
BOR with 1
CXOR with 1
DNOT operation
✗ Incorrect
AND with 1 isolates the least significant bit, which determines even or odd.
What does the expression (num & 1) == 0 mean?
Anum is even
Bnum is negative
Cnum is odd
Dnum is zero
✗ Incorrect
If the least significant bit is 0, the number is even.
What is the binary representation of the number 7?
A101
B111
C110
D100
✗ Incorrect
7 in binary is 111.
If num = 14, what is the result of num & 1?
A15
B1
C14
D0
✗ Incorrect
14 in binary ends with 0, so num & 1 = 0.
Why might bitwise operations be preferred over modulo for checking even/odd?
ABitwise is slower
BBitwise uses less memory
CBitwise is faster and simpler
DModulo is more accurate
✗ Incorrect
Bitwise operations are faster because they directly manipulate bits without division.
Explain how to check if a number is even or odd using bitwise operations.
Think about the last bit in binary.
You got /4 concepts.
Write a simple Go function that prints whether a number is even or odd using bits.
Use num & 1 to decide.
You got /4 concepts.
Practice
(1/5)
1. What does the expression num & 1 check when determining if a number is even or odd?
easy
A. It checks the least significant bit to see if it is 1 (odd) or 0 (even).
B. It checks if the number is greater than 1.
C. It divides the number by 2.
D. It adds 1 to the number.
Solution
Step 1: Understand bitwise AND operation
The expression num & 1 performs a bitwise AND between the number and 1, which isolates the least significant bit (rightmost bit).
Step 2: Interpret the least significant bit
If the least significant bit is 1, the number is odd; if it is 0, the number is even.
Final Answer:
It checks the least significant bit to see if it is 1 (odd) or 0 (even). -> Option A
Quick Check:
num & 1 = 1 means odd, 0 means even [OK]
Hint: Check last bit with & 1; 1 means odd, 0 means even [OK]
Common Mistakes:
Thinking & 1 divides the number
Confusing bitwise AND with addition
Assuming it checks if number > 1
2. Which of the following Go code snippets correctly checks if an integer n is odd using bitwise operations?
easy
A. if n & 0 == 1 { fmt.Println("Odd") }
B. if n | 1 == 1 { fmt.Println("Odd") }
C. if n ^ 1 == 1 { fmt.Println("Odd") }
D. if n & 1 == 1 { fmt.Println("Odd") }
Solution
Step 1: Analyze bitwise AND usage
The correct way to check oddness is using bitwise AND with 1: n & 1. This isolates the least significant bit.
Step 2: Check condition correctness
if n & 1 == 1 { fmt.Println("Odd") } uses n & 1 == 1, which is true if the number is odd. Other options use incorrect operators or values.
Final Answer:
if n & 1 == 1 { fmt.Println("Odd") } -> Option D
Quick Check:
Use & 1 == 1 to check odd [OK]
Hint: Use 'if n & 1 == 1' to check odd numbers in Go [OK]
Common Mistakes:
Using bitwise OR instead of AND
Comparing with 0 instead of 1
Using XOR which changes bits
3. What will be the output of the following Go code?
package main
import "fmt"
func main() {
n := 14
if n & 1 == 0 {
fmt.Println("Even")
} else {
fmt.Println("Odd")
}
}
medium
A. Even
B. Odd
C. 14
D. Compilation error
Solution
Step 1: Evaluate bitwise AND for n=14
14 in binary is 1110. The least significant bit is 0. So, 14 & 1 = 0.
Step 2: Check the if condition
Since 14 & 1 == 0, the condition is true, so it prints "Even".
Final Answer:
Even -> Option A
Quick Check:
14 & 1 = 0 means Even [OK]
Hint: If n & 1 == 0, number is even [OK]
Common Mistakes:
Assuming 14 is odd
Confusing bitwise AND with arithmetic AND
Expecting output to be the number itself
4. Find the error in this Go code snippet that tries to print if a number is even or odd:
package main
import "fmt"
func main() {
num := 7
if num & 1 = 1 {
fmt.Println("Odd")
} else {
fmt.Println("Even")
}
}
medium
A. Using bitwise AND instead of OR
B. Using single '=' instead of '==' in if condition
C. Missing import statement
D. Variable 'num' not declared
Solution
Step 1: Identify the if condition syntax
The condition uses single '=' which is assignment, not comparison. It should be '==' to compare values.
Step 2: Understand Go syntax rules
Go requires '==' for equality checks. Using '=' in if condition causes a compilation error.
Final Answer:
Using single '=' instead of '==' in if condition -> Option B
Quick Check:
Use '==' for comparison, not '=' [OK]
Hint: Use '==' for comparison in if statements [OK]
Common Mistakes:
Using '=' instead of '==' in conditions
Confusing bitwise AND with logical AND
Forgetting to declare variables
5. You want to write a Go function IsEven that returns true if a number is even and false if odd, using bitwise operations. Which of the following implementations is correct?
Even numbers have least significant bit 0, so n & 1 equals 0 for even numbers.
Step 2: Verify the correct implementation
func IsEven(n int) bool { return n & 1 == 0 } is correct because even numbers have LSB 0. The implementation returning n & 1 == 1 checks for odd. The bitwise OR and right shift implementations are incorrect for checking evenness.
Final Answer:
func IsEven(n int) bool { return n & 1 == 0 } -> Option C
Quick Check:
Return true if n & 1 == 0 for even [OK]
Hint: Return n & 1 == 0 to check even numbers [OK]
Common Mistakes:
Returning true for odd numbers instead of even
Using right shift incorrectly
Using bitwise OR instead of AND
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.