Bit manipulation means working directly with the 0s and 1s inside a number to change or check its value quickly.
Click to reveal answer
beginner
Why can bit manipulation be faster than normal arithmetic?
Because computers handle bits directly in hardware, bit operations like AND, OR, and shifts are usually quicker than adding or multiplying numbers.
Click to reveal answer
beginner
Which bit operation can be used to multiply a number by 2 quickly?
Left shift (<<) by 1. For example, 5 << 1 equals 10, which is 5 multiplied by 2.
Click to reveal answer
intermediate
When is it better to use arithmetic instead of bit manipulation?
When code clarity and readability matter more than speed, or when working with numbers that are not integers, arithmetic is usually better.
Click to reveal answer
intermediate
Give an example where bit manipulation beats arithmetic in real life.
Checking if a number is even or odd using (number & 1) is faster than using (number % 2).
Click to reveal answer
Which bit operation can quickly check if a number is odd?
AAND with 1
BOR with 1
CXOR with 1
DLeft shift by 1
✗ Incorrect
AND with 1 checks the last bit; if it is 1, the number is odd.
What does the left shift operator (<<) do to a number?
ASubtracts 1 from the number
BDivides the number by 2
CAdds 1 to the number
DMultiplies the number by 2
✗ Incorrect
Left shifting by 1 moves bits left, doubling the number.
Why might bit manipulation be preferred in embedded systems?
ABecause it uses less memory
BBecause it is easier to read
CBecause it runs faster on limited hardware
DBecause it supports floating point numbers
✗ Incorrect
Embedded systems often have limited power and speed, so fast bit operations help.
Which of these is NOT a common bit operation?
AAND
BMODULO
COR
DXOR
✗ Incorrect
Modulo (%) is an arithmetic operation, not a bit operation.
When is arithmetic better than bit manipulation?
AWhen working with floating point numbers
BWhen speed is critical
CWhen working with integers only
DWhen checking bits
✗ Incorrect
Bit manipulation works only on integers; arithmetic handles floats.
Explain why bit manipulation can be faster than arithmetic operations.
Think about how computers store and process numbers.
You got /4 concepts.
Describe a situation where using bit manipulation is better than using arithmetic.
Focus on simple, fast checks or calculations.
You got /4 concepts.
Practice
(1/5)
1. Why is bit manipulation often faster than arithmetic operations like multiplication or division?
easy
A. Because bit manipulation works directly on binary digits using CPU instructions
B. Because arithmetic operations use less memory
C. Because bit manipulation requires more CPU cycles
D. Because arithmetic operations are always done in parallel
Solution
Step 1: Understand how bit manipulation works
Bit manipulation uses CPU instructions that directly change bits (0s and 1s) in numbers.
Step 2: Compare with arithmetic operations
Arithmetic like multiplication or division involves more complex calculations and takes more CPU cycles.
Final Answer:
Because bit manipulation works directly on binary digits using CPU instructions -> Option A
Quick Check:
Bit manipulation = faster CPU-level operations [OK]
Hint: Bit ops use CPU bits directly, so they run faster [OK]
Common Mistakes:
Thinking arithmetic uses less memory
Believing bit manipulation is slower
Assuming arithmetic is always parallel
2. Which of the following is the correct JavaScript syntax to check if the 3rd bit (from right, zero-based) of a number n is set using bit manipulation?
easy
A. if ((n & (1 << 3)) !== 0) { /* bit is set */ }
B. if ((n | (1 << 3)) === 0) { /* bit is set */ }
C. if ((n & (3 << 1)) !== 0) { /* bit is set */ }
D. if ((n ^ (1 << 3)) !== 0) { /* bit is set */ }
Solution
Step 1: Understand bit checking syntax
To check if a bit is set, use bitwise AND (&) with 1 shifted left by the bit position.
Step 2: Apply to 3rd bit (zero-based)
Shift 1 by 3 places: (1 << 3). Then check if (n & (1 << 3)) is not zero.
Final Answer:
if ((n & (1 << 3)) !== 0) { /* bit is set */ } -> Option A
Quick Check:
Bit check = (n & (1 << 3)) !== 0 [OK]
Hint: Use & with 1 shifted to bit position to check bit [OK]
Common Mistakes:
Using bitwise OR (|) instead of AND (&)
Shifting wrong number of bits
Using XOR (^) which toggles bits
3. What is the output of the following JavaScript code?
let x = 5; // binary 0101
let y = x << 2;
console.log(y);
medium
A. 10
B. 40
C. 20
D. 8
Solution
Step 1: Understand left shift operation
Left shifting by 2 moves bits 2 places left, multiplying the number by 2^2 = 4.
Step 2: Calculate shifted value
5 * 4 = 20, so y = 20.
Final Answer:
20 -> Option C
Quick Check:
5 << 2 = 20 [OK]
Hint: Left shift by n multiplies by 2^n [OK]
Common Mistakes:
Confusing left shift with right shift
Forgetting shift multiplies by powers of two
Assuming shift adds bits instead of moving
4. Find the error in this JavaScript code that tries to clear the 2nd bit of a number num:
num = num & (1 << 2);
medium
A. It should use bitwise OR (|) instead of AND (&)
B. It should use bitwise XOR (^) to clear the bit
C. There is no error; code correctly clears the bit
D. It should use bitwise AND with the negation of the bit mask
Solution
Step 1: Understand clearing a bit
To clear a bit, AND with a mask that has all bits 1 except the target bit 0.
Step 2: Fix the mask
Mask should be ~(1 << 2), so code should be: num = num & ~(1 << 2).
Final Answer:
It should use bitwise AND with the negation of the bit mask -> Option D
Quick Check:
Clear bit = num & ~(1 << bit) [OK]
Hint: Clear bit by AND with negated mask ~(1 << bit) [OK]
Common Mistakes:
Using OR (|) which sets bits instead of clearing
Using XOR (^) which toggles bits
Not negating the mask before AND
5. You want to quickly check if a number n is even or odd without using modulus (%) or division. Which bit manipulation expression correctly does this and why is it faster than arithmetic?
hard
A. (n ^ 1) === 0 because XOR toggles the last bit
B. (n & 1) === 0 because it checks the least significant bit directly
C. (n | 1) === n because OR sets the last bit
D. (n >> 1) === 0 because shifting right divides by 2
Solution
Step 1: Understand even/odd check with bits
The least significant bit (rightmost) is 0 for even numbers and 1 for odd numbers.
Step 2: Use bitwise AND to check LSB
Expression (n & 1) extracts the last bit. If it equals 0, number is even.
Step 3: Why faster than arithmetic
Bitwise AND is a simple CPU instruction, faster than modulus or division.
Final Answer:
(n & 1) === 0 because it checks the least significant bit directly -> Option B
Quick Check:
Even check = (n & 1) === 0 [OK]
Hint: Check even with (n & 1) === 0 for fast bit test [OK]
Common Mistakes:
Using right shift (>>) which changes number value
Using OR (|) which sets bits instead of checking
Using XOR (^) which toggles bits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.