What is the output of (6 & 1) and what does it mean?
6 in binary is 110. (6 & 1) = 0, meaning 6 is even.
Click to reveal answer
intermediate
Can this bitwise method be used for negative numbers to check even or odd? Why?
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 operation helps determine if a number is even or odd?
AAND with 1
BOR with 1
CXOR with 1
DNOT operation
✗ Incorrect
AND with 1 checks the least significant bit to determine even or odd.
What does the expression (num & 1) == 0 indicate?
Anum is odd
Bnum is negative
Cnum is even
Dnum is zero
✗ Incorrect
If the least significant bit is 0, the number is even.
What is the binary representation of the number 5 and what is (5 & 1)?
A101, 1
B101, 0
C110, 1
D110, 0
✗ Incorrect
5 in binary is 101; (5 & 1) = 1 means 5 is odd.
Is the bitwise method for checking even or odd faster than using modulo (%) operator?
ANo, modulo is faster
BYes, because bitwise operations are faster
CBoth are equally fast
DDepends on the number
✗ Incorrect
Bitwise operations are generally faster than modulo operations.
Does the bitwise check for even or odd work correctly for negative numbers?
AOnly for powers of two
BNo, it only works for positive numbers
COnly for zero
DYes, because two's complement preserves LSB
✗ Incorrect
Two's complement representation keeps the least significant bit consistent for even/odd.
Explain how to check if a number is even or odd using bitwise operations.
Think about the last binary digit of the number.
You got /4 concepts.
Write a simple Java code snippet that prints 'Even' or 'Odd' using bitwise operations.
Use if ((num & 1) == 1) to check odd.
You got /3 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 if the least significant bit of num is 1, indicating odd number
B. It checks if num is greater than 1
C. It shifts num one bit to the right
D. It adds 1 to num
Solution
Step 1: Understand bitwise AND operation
The expression num & 1 performs a bitwise AND between num and 1, which isolates the least significant bit (rightmost bit) of num.
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 if the least significant bit of num is 1, indicating odd number -> Option A
Quick Check:
num & 1 = 1 means odd [OK]
Hint: Check last bit with & 1; 1 means odd, 0 means even [OK]
Common Mistakes:
Thinking & 1 shifts bits instead of checking last bit
Confusing bitwise AND with addition
Assuming & 1 checks if number is greater than 1
2. Which of the following Java code snippets correctly checks if an integer num is even using bitwise operators?
easy
A. if ((num & 0) == 1) { System.out.println("Even"); }
B. if ((num | 1) == 0) { System.out.println("Even"); }
C. if ((num & 1) == 0) { System.out.println("Even"); }
D. if ((num ^ 1) == 0) { System.out.println("Even"); }
Solution
Step 1: Analyze bitwise AND with 1
The expression num & 1 isolates the least significant bit. If it equals 0, the number is even.
Step 2: Check correctness of conditions
if ((num & 1) == 0) { System.out.println("Even"); } correctly checks if (num & 1) == 0 to print "Even". Other options misuse bitwise OR, AND with 0, or XOR, which do not correctly check evenness.
Final Answer:
if ((num & 1) == 0) { System.out.println("Even"); } -> Option C
Quick Check:
num & 1 == 0 means even [OK]
Hint: Use (num & 1) == 0 to check even numbers [OK]
Common Mistakes:
Using bitwise OR instead of AND
Comparing with wrong values like 1 when checking even
Using XOR incorrectly for even check
3. What is the output of the following Java code?
int num = 7;
if ((num & 1) == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
medium
A. Compilation error
B. Even
C. 0
D. Odd
Solution
Step 1: Calculate bitwise AND of num and 1
7 in binary is 0111. 0111 & 0001 = 0001, which equals 1.
Step 2: Check condition and print output
Since (num & 1) == 0 is false, the else block executes, printing "Odd".
Final Answer:
Odd -> Option D
Quick Check:
7 & 1 = 1 means odd [OK]
Hint: If num & 1 equals 1, print Odd; else Even [OK]
Common Mistakes:
Assuming 7 is even
Confusing bitwise AND with arithmetic AND
Expecting output 0 or error
4. Identify the error in the following Java code that tries to check if a number is odd using bits:
int num = 10;
if (num & 1 == 1) {
System.out.println("Odd");
} else {
System.out.println("Even");
}
medium
A. No error, code is correct
B. Missing parentheses around (num & 1)
C. Incorrect variable type for num
D. Using bitwise OR instead of AND
Solution
Step 1: Analyze operator precedence in condition
In Java, == has higher precedence than &. So num & 1 == 1 is treated as num & (1 == 1).
Step 2: Fix condition with parentheses
Since (1 == 1) is true (boolean), num & true is invalid. Correct syntax is (num & 1) == 1.
Final Answer:
Missing parentheses around (num & 1) -> Option B
Quick Check:
Use parentheses to ensure correct bitwise operation [OK]
Hint: Always parenthesize bitwise ops before comparison [OK]
5. You want to write a Java method isEven(int num) that returns true if num is even and false if odd, using bitwise operators. Which implementation is correct?
hard
A. public boolean isEven(int num) { return (num & 1) == 0; }
B. public boolean isEven(int num) { return (num | 1) == 0; }
C. public boolean isEven(int num) { return (num ^ 1) == 0; }
D. public boolean isEven(int num) { return (num & 0) == 1; }
Solution
Step 1: Understand bitwise check for even
Even numbers have least significant bit 0, so (num & 1) == 0 returns true for even numbers.
Step 2: Evaluate each option
public boolean isEven(int num) { return (num & 1) == 0; } correctly returns true if num is even. Options A, B, and D use incorrect bitwise operations that do not reliably check evenness.
Final Answer:
public boolean isEven(int num) { return (num & 1) == 0; } -> Option A
Quick Check:
Use (num & 1) == 0 to return true for even [OK]
Hint: Return (num & 1) == 0 for even check in method [OK]
Common Mistakes:
Using bitwise OR or XOR incorrectly
Comparing with wrong values
Returning wrong boolean for even check
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.