Concept Flow - Check if Number is Even or Odd Using Bits
Start with number n
Check last bit: n & 1
Output Even
End
We check the last bit of the number using bitwise AND with 1. If last bit is 0, number is even; if 1, number is odd.
func isEven(n int) bool { return (n & 1) == 0 } // Example: n = 5 // 5 in binary: 0101 // 5 & 1 = 1 (odd)
| Step | Operation | Number (n) | Bitwise AND (n & 1) | Result | Output |
|---|---|---|---|---|---|
| 1 | Start with n | 5 (0101) | - | - | - |
| 2 | Calculate n & 1 | 5 (0101) | 1 (0001) | 1 | Odd |
| 3 | Check if (n & 1) == 0 | 5 (0101) | 1 | False | Odd |
| 4 | Return result | 5 (0101) | - | - | False (odd) |
| 5 | End | - | - | - | - |
| Variable | Start | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|
| n | 5 | 5 | 5 | 5 |
| n & 1 | - | 1 | 1 | 1 |
| isEven | - | - | false | false |
Check if number is even or odd using bitwise AND: - Use expression (n & 1) - If result is 0, number is even - If result is 1, number is odd - This checks the last binary bit efficiently - Faster than modulo operator
num & 1 check when determining if a number is even or odd?num & 1 performs a bitwise AND between the number and 1, which isolates the least significant bit (rightmost bit).n is odd using bitwise operations?n & 1. This isolates the least significant bit.n & 1 == 1, which is true if the number is odd. Other options use incorrect operators or values.package main
import "fmt"
func main() {
n := 14
if n & 1 == 0 {
fmt.Println("Even")
} else {
fmt.Println("Odd")
}
}package main
import "fmt"
func main() {
num := 7
if num & 1 = 1 {
fmt.Println("Odd")
} else {
fmt.Println("Even")
}
}IsEven that returns true if a number is even and false if odd, using bitwise operations. Which of the following implementations is correct?n & 1 equals 0 for even numbers.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.Pass the test and this pattern moves unknown