Bit manipulation is the process of directly working with the individual bits of data, using operations like AND, OR, XOR, NOT, shifts, etc., to perform fast and efficient calculations.
Click to reveal answer
beginner
Why is bit manipulation often faster than arithmetic operations?
Bit manipulation uses simple CPU instructions that work directly on bits, which are faster and use less memory than arithmetic operations like multiplication or division.
Click to reveal answer
intermediate
Give an example when bit manipulation beats arithmetic.
Multiplying or dividing by powers of two can be done faster using bit shifts. For example, x * 8 can be done as x << 3, which is faster than normal multiplication.
Click to reveal answer
beginner
What is a common use case of bit manipulation in programming?
Bit manipulation is commonly used in low-level programming, such as setting flags, masking bits, encoding data, or optimizing performance-critical code.
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?
ARight shift by 2 (x >> 2)
BLeft 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.
Why is bit manipulation faster than normal arithmetic?
ABecause it uses complex CPU instructions
BBecause it avoids using registers
CBecause it uses more memory
DBecause it works directly on bits with simple CPU instructions
✗ Incorrect
Bit manipulation uses simple CPU instructions that operate directly on bits, making it faster and more efficient.
Which operation clears (sets to 0) a specific bit in a number?
ABitwise AND with 0 at that bit
BBitwise XOR with 1 at that bit
CBitwise OR with 1 at that bit
DBitwise NOT of the number
✗ Incorrect
Bitwise AND with 0 at the target bit clears that bit to 0, while leaving others unchanged.
When is it better to avoid bit manipulation?
AWhen code readability is more important
BWhen performance is critical
CWhen working with low-level hardware
DWhen manipulating flags
✗ Incorrect
Bit manipulation can make code harder to read, so avoid it when readability and maintainability matter more.
Which of these is NOT a bit manipulation operation?
ABitwise XOR
BBitwise AND
CAddition
DLeft shift
✗ Incorrect
Addition is an arithmetic operation, not a bit manipulation operation.
Explain why bit manipulation can be faster than arithmetic operations and give an example.
Think about how CPUs handle bits versus numbers.
You got /3 concepts.
Describe a situation where using bit manipulation is helpful and when it might be better to avoid it.
Consider both pros and cons of bit manipulation.
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 works directly on binary digits using CPU instructions
B. Because arithmetic operations use more memory than bit manipulation
C. Because bit manipulation requires more complex algorithms
D. Because arithmetic operations are not supported by modern CPUs
Solution
Step 1: Understand how bit manipulation works
Bit manipulation uses CPU instructions that directly change bits, which are the smallest data units.
Step 2: Compare with arithmetic operations
Arithmetic like multiplication or division involves multiple CPU cycles and more complex calculations.
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, arithmetic uses slower math [OK]
Common Mistakes:
Thinking bit manipulation uses more memory
Believing arithmetic is always faster
Confusing bit manipulation with complex algorithms
2. Which of the following TypeScript expressions correctly uses bit manipulation to multiply a number by 4?
easy
A. num & 4
B. num >> 2
C. num << 2
D. num | 4
Solution
Step 1: Recall bit shift left operation
Shifting bits left by 1 multiplies the number by 2, so shifting left by 2 multiplies by 4.
Step 2: Check each option
num << 2 shifts left by 2 bits, correctly multiplying by 4. num >> 2 performs right shift (division by 4), num & 4 is bitwise AND, and num | 4 is bitwise OR; none multiply by 4.
Final Answer:
num << 2 -> Option C
Quick Check:
Left shift by 2 = multiply by 4 [OK]
Hint: Left shift by n bits multiplies by 2^n [OK]
Common Mistakes:
Using right shift instead of left shift
Confusing bitwise AND/OR with multiplication
Forgetting shift counts start at zero
3. What is the output of the following TypeScript code?
let x = 10;
let y = x & 6;
console.log(y);
medium
A. 2
B. 6
C. 10
D. 14
Solution
Step 1: Convert numbers to binary
10 in binary is 1010, 6 in binary is 0110.
Step 2: Perform bitwise AND
1010 & 0110 = 0010 (binary), which is 2 in decimal.
Final Answer:
2 -> Option A
Quick Check:
10 & 6 = 2 [OK]
Hint: AND keeps bits set in both numbers only [OK]
Common Mistakes:
Confusing AND with OR operation
Not converting binary correctly
Assuming output is decimal sum
4. Identify the error in this TypeScript code that tries to check if a number is even using bit manipulation:
B. The condition should check if (num & 1) === 0 to detect even numbers
C. The function should return num % 2 === 1 instead
D. Bitwise AND cannot be used to check even numbers
Solution
Step 1: Understand bitwise check for even numbers
Even numbers have last bit 0, so (num & 1) equals 0 for even numbers.
Step 2: Analyze the condition
The code returns true if (num & 1) === 1, which means odd numbers, so it is reversed.
Final Answer:
The condition should check if (num & 1) === 0 to detect even numbers -> Option B
Quick Check:
Even check = (num & 1) === 0 [OK]
Hint: Even numbers have last bit 0, check (num & 1) === 0 [OK]
Common Mistakes:
Checking for 1 instead of 0 for even
Thinking bitwise AND can't check parity
Using modulo instead of bitwise unnecessarily
5. You want to quickly double a number many times in a performance-critical loop. Which approach is best and why?
function doubleManyTimes(num: number, times: number): number {
let result = num;
for (let i = 0; i < times; i++) {
// Which line is best here?
// A: result = result * 2;
// B: result = result << 1;
}
return result;
}
hard
A. Use division by 0.5 (result = result / 0.5) because it's mathematically equivalent
B. Use multiplication (result = result * 2) because it's clearer
C. Use addition (result = result + result) because it's the simplest
D. Use bit shift left (result = result << 1) because it's faster and uses less CPU cycles
Solution
Step 1: Compare multiplication and bit shift performance
Bit shift left is a CPU-level operation that is faster than multiplication, especially in loops.
Step 2: Evaluate other options
Addition doubles but is slower than bit shift; division by 0.5 is slower and less clear.
Final Answer:
Use bit shift left (result = result << 1) because it's faster and uses less CPU cycles -> Option D
Quick Check:
Bit shift left = faster multiply by 2 [OK]
Hint: Use << 1 to multiply by 2 faster in loops [OK]
Common Mistakes:
Choosing multiplication for clarity over speed
Using addition which is slower than bit shift
Using division which is inefficient
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.