Bit manipulation is the process of directly working with the individual bits of a number using operations like AND, OR, XOR, NOT, shifts, etc.
Click to reveal answer
beginner
Why is bit manipulation faster than arithmetic operations?
Bit manipulation uses simple CPU instructions that work directly on bits, making them faster and more efficient than arithmetic operations like multiplication or division.
Click to reveal answer
intermediate
Give an example when bit manipulation beats arithmetic.
Using left shift (<<) to multiply by 2 is faster than using the * operator. For example, x << 1 equals x * 2 but runs faster.
Click to reveal answer
intermediate
What is a common use case for bit manipulation in programming?
Bit manipulation is often used for setting, clearing, or toggling flags, working with permissions, or optimizing calculations in low-level programming.
Click to reveal answer
intermediate
When should you avoid using bit manipulation?
Avoid bit manipulation when code readability and maintainability are more important than performance, or when the problem does not involve bit-level data.
Click to reveal answer
Which bit operation can be used to multiply a number by 4 quickly?
ALeft shift by 2 (x << 2)
BRight shift by 2 (x >> 2)
CBitwise AND with 4 (x & 4)
DBitwise OR with 4 (x | 4)
✗ Incorrect
Left shifting by 2 moves bits two places left, which multiplies the number by 2^2 = 4.
What does the bitwise AND operation (&) do?
ASets bits to 1 if either bit is 1
BSets bits to 1 if both bits are 1
CFlips all bits
DShifts bits to the left
✗ Incorrect
Bitwise AND sets a bit to 1 only if both corresponding bits are 1.
Why might bit manipulation be preferred over arithmetic in embedded systems?
ABecause it runs faster and uses less power
BBecause it uses less memory
CBecause it is easier to read
DBecause it supports floating point numbers
✗ Incorrect
Bit manipulation uses simple CPU instructions that are faster and consume less power, important in embedded systems.
Which operation toggles bits (flips 0 to 1 and 1 to 0)?
ABitwise AND (&)
BBitwise OR (|)
CBitwise XOR (^)
DBitwise NOT (~)
✗ Incorrect
Bitwise XOR with 1 flips bits, toggling them.
What is a downside of using bit manipulation?
AIt is always slower than arithmetic
BIt uses more memory
CIt only works on floating point numbers
DIt can make code harder to understand
✗ Incorrect
Bit manipulation can make code less readable and harder to maintain.
Explain why bit manipulation can be faster than arithmetic operations and give an example.
Think about how CPUs handle bits directly.
You got /3 concepts.
Describe common scenarios where bit manipulation is useful and when it should be avoided.
Consider both benefits and trade-offs.
You got /3 concepts.
Practice
(1/5)
1. Why is bit manipulation often faster than arithmetic operations like multiplication or division?
easy
A. Because bit manipulation uses more memory which speeds up processing
B. Because arithmetic operations are not supported by modern CPUs
C. Because bit manipulation uses simple CPU instructions that run in constant time
D. Because arithmetic operations always cause errors in calculations
Solution
Step 1: Understand CPU instruction speed
Bit manipulation uses basic CPU instructions like AND, OR, XOR, shifts which are very fast and usually take one CPU cycle.
Step 2: Compare with arithmetic operations
Multiplication and division are more complex and take multiple CPU cycles, making them slower than bit operations.
Final Answer:
Because bit manipulation uses simple CPU instructions that run in constant time -> Option C
Quick Check:
Bit ops faster than arithmetic [OK]
Hint: Bit ops use simple CPU steps, so they run faster [OK]
Common Mistakes:
Thinking arithmetic is always faster
Confusing memory use with speed
Believing CPUs don't support arithmetic
2. Which of the following Go code snippets correctly uses bit manipulation to multiply an integer by 8?
easy
A. result := num >> 3
B. result := num << 3
C. result := num & 8
D. result := num ^ 8
Solution
Step 1: Understand bit shift left operation
Shifting bits left by 3 positions multiplies the number by 2^3 = 8.
Step 2: Check other operations
Right shift divides, AND masks bits, XOR toggles bits, so they don't multiply by 8.
Final Answer:
result := num << 3 -> Option B
Quick Check:
Left shift by 3 multiplies by 8 [OK]
Hint: Use << 3 to multiply by 8 quickly [OK]
Common Mistakes:
Using >> instead of << for multiplication
Using & or ^ which do not multiply
Confusing shift count with multiplication factor
3. What is the output of the following Go code?
num := 12
result := (num & 7) << 1
fmt.Println(result)
medium
A. 8
B. 10
C. 14
D. 24
Solution
Step 1: Calculate num & 7
12 in binary is 1100, 7 is 0111. Bitwise AND: 1100 & 0111 = 0100 (decimal 4).
Step 2: Shift left by 1
4 << 1 means 4 * 2 = 8.
Final Answer:
8 -> Option A
Quick Check:
(12 & 7) << 1 = 8 [OK]
Hint: AND then shift left doubles the masked value [OK]
Common Mistakes:
Misreading binary AND result
Confusing left shift with right shift
Calculating decimal incorrectly
4. Find the error in this Go code that tries to check if a number is even using bit manipulation:
func isEven(num int) bool {
return num & 1 == 0
}
medium
A. Missing parentheses around bitwise operation
B. Incorrect operator precedence causes wrong result
C. Bitwise AND cannot be used with integers
D. No error, code works correctly
Solution
Step 1: Understand operator precedence in Go
Bitwise AND (&) has higher precedence than ==, so expression evaluates as (num & 1) == 0 correctly.
Step 2: Check if code logic is correct
Checking if last bit is 0 means number is even, so function returns true for even numbers.
Final Answer:
No error, code works correctly -> Option D
Quick Check:
Bitwise AND with 1 checks evenness correctly [OK]
Hint: Bitwise AND with 1 checks last bit correctly [OK]
Common Mistakes:
Assuming operator precedence error
Thinking bitwise AND can't be used with ints
Adding unnecessary parentheses
5. You want to quickly check if a number is a power of two using bit manipulation in Go. Which expression correctly does this?
func isPowerOfTwo(num int) bool {
return ???
}
hard
A. num & (num - 1) == 0 && num != 0
B. num | (num - 1) == 0
C. num ^ (num - 1) == 0
D. num & (num + 1) == 0
Solution
Step 1: Recall power of two property
A power of two has exactly one bit set. num & (num - 1) clears the lowest set bit.
Step 2: Check expression meaning
If num is power of two, num & (num - 1) == 0. Also check num != 0 to exclude zero.
Step 3: Verify other options
OR, XOR, and & with (num + 1) do not correctly test power of two.
Final Answer:
num & (num - 1) == 0 && num != 0 -> Option A
Quick Check:
Power of two test uses num & (num-1) = 0 [OK]
Hint: Use num & (num-1) == 0 to test power of two [OK]
Common Mistakes:
Using OR or XOR instead of AND
Not checking num != 0
Using num + 1 instead of num - 1
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.