What does the AND (&) bitwise operator do between two bits?
The AND operator returns 1 only if both bits are 1; otherwise, it returns 0. Example: 1 & 1 = 1, 1 & 0 = 0, 0 & 0 = 0.
Click to reveal answer
beginner
Explain the OR (|) bitwise operator in simple terms.
The OR operator returns 1 if at least one of the bits is 1; it returns 0 only if both bits are 0. Example: 1 | 0 = 1, 0 | 0 = 0.
Click to reveal answer
beginner
What is the result of XOR (^) between two bits?
XOR returns 1 if the bits are different, and 0 if they are the same. Example: 1 ^ 0 = 1, 1 ^ 1 = 0.
Click to reveal answer
beginner
What does the NOT (~) operator do to a bit?
The NOT operator flips the bit: 1 becomes 0, and 0 becomes 1. Example: ~1 = 0, ~0 = 1.
Click to reveal answer
beginner
What happens when you perform a left shift (<<) on a binary number?
Left shift moves all bits to the left by the given number of positions, adding zeros on the right. This is like multiplying the number by 2 for each shift. Example: 0010 (2) << 1 = 0100 (4).
Click to reveal answer
What is the result of 5 & 3 in binary? (5 = 0101, 3 = 0011)
A0001
B0011
C0101
D0111
✗ Incorrect
5 & 3 = 0101 & 0011 = 0001 (which is 1 in decimal).
Which operator returns 1 only when bits are different?
AXOR (^)
BAND (&)
COR (|)
DNOT (~)
✗ Incorrect
XOR returns 1 if bits differ, 0 if they are the same.
What does the right shift (>>) operator do?
AMoves bits to the left, adding zeros on the right
BMoves bits to the right, discarding bits on the right
CFlips all bits
DMoves bits to the right, adding zeros on the left
✗ Incorrect
Right shift moves bits right and discards bits on the right (logical shift adds zeros on the left).
What is the result of ~0 in binary (assuming 4 bits)?
A1000
B0000
C1111
D0111
✗ Incorrect
~0 flips all bits: 0000 becomes 1111.
Which bitwise operator can be used to clear specific bits to zero?
ANOT (~)
BOR (|)
CXOR (^)
DAND (&)
✗ Incorrect
AND with 0 clears bits to zero; AND with 1 keeps bits unchanged.
Describe how the AND, OR, and XOR bitwise operators work with simple examples.
Think about how bits combine in each operator.
You got /4 concepts.
Explain what happens when you perform left and right bit shifts on a binary number.
Imagine sliding bits along a row and filling empty spots.
You got /4 concepts.
Practice
(1/5)
1. What is the result of the bitwise AND operation between 6 and 3? (6 = 110 in binary, 3 = 011 in binary)
easy
A. 2
B. 1
C. 3
D. 0
Solution
Step 1: Convert numbers to binary
6 in binary is 110, 3 in binary is 011.
Step 2: Perform bitwise AND
110 AND 011 = 010 in binary, which is 2 in decimal.
Final Answer:
2 -> Option A
Quick Check:
6 & 3 = 2 [OK]
Hint: AND keeps bits set in both numbers only [OK]
Common Mistakes:
Confusing AND with OR operation
Not converting numbers to binary before operation
Mixing decimal and binary values
2. Which of the following is the correct syntax in Go to perform a bitwise XOR between variables a and b?
easy
A. c := a & b
B. c := a && b
C. c := a ^ b
D. c := a | b
Solution
Step 1: Recall Go bitwise operators
In Go, '^' is the bitwise XOR operator.
Step 2: Match syntax
c := a ^ b uses '^' correctly for XOR. Other options use logical AND (&&), OR (|), and AND (&).
Final Answer:
c := a ^ b -> Option C
Quick Check:
Bitwise XOR uses '^' in Go [OK]
Hint: XOR operator in Go is '^' symbol [OK]
Common Mistakes:
Using '&&' which is logical AND, not bitwise
Confusing OR '|' with XOR '^'
Using '&' which is bitwise AND, not XOR
3. What is the output of the following Go code?
package main
import "fmt"
func main() {
var x uint8 = 12 // binary 00001100
var y uint8 = 5 // binary 00000101
fmt.Println(x | y)
fmt.Println(x & y)
fmt.Println(x ^ y)
}
Hint: OR sets bits from either number, AND only common bits [OK]
Common Mistakes:
Mixing up OR and XOR results
Incorrect binary conversion
Confusing decimal output values
4. The following Go code is intended to left shift the number 3 by 2 bits. Identify the error.
package main
import "fmt"
func main() {
var a int = 3
var b int = 2
var c int = a << b
fmt.Println(c)
}
medium
A. Error: shift count must be unsigned integer
B. No error, output is 12
C. Error: cannot shift int by int
D. Error: missing parentheses around shift
Solution
Step 1: Understand Go shift rules
In Go, shift count must be unsigned integer or untyped constant. Here, b is int but allowed.
Step 2: Calculate left shift
3 << 2 means shifting bits of 3 (00000011) left by 2 bits: 00001100 = 12 decimal.
Final Answer:
No error, output is 12 -> Option B
Quick Check:
3 << 2 = 12 [OK]
Hint: Shift count can be int variable in Go if within range [OK]
Common Mistakes:
Assuming shift count must be uint explicitly
Thinking parentheses are required around shift
Confusing shift operators with arithmetic operators
5. Given a 8-bit unsigned integer n, write a Go expression using bitwise operators to toggle (flip) the 3rd bit (counting from 0, right to left) of n. Which expression correctly does this?
hard
A. n ^ (1 << 3)
B. n & (1 << 3)
C. n | (1 << 3)
D. ~n & (1 << 3)
Solution
Step 1: Understand toggling a bit
Toggling means flipping the bit: if 0 becomes 1, if 1 becomes 0.
Step 2: Use XOR to toggle
XOR with 1 flips the bit, XOR with 0 keeps it same. So, n ^ (1 << 3) flips the 3rd bit.
Step 3: Check other options
AND keeps bit only if both are 1 (no toggle), OR sets bit to 1 (no toggle), NOT and AND combination does not toggle.
Final Answer:
n ^ (1 << 3) -> Option A
Quick Check:
XOR with mask toggles bit [OK]
Hint: XOR with 1 flips bit, XOR with 0 keeps bit same [OK]
Common Mistakes:
Using AND or OR which don't toggle bits
Confusing NOT operator usage
Incorrect bit position shift
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.