What does it mean for a number to be a power of two?
A number is a power of two if it can be written as 2 raised to an integer exponent, like 1, 2, 4, 8, 16, etc.
Click to reveal answer
intermediate
How can you check if a number is a power of two using bitwise operations?
If a number n is a power of two, then n & (n - 1) equals 0. This works because powers of two have only one '1' bit in binary.
Click to reveal answer
advanced
Why does the expression (n & (n - 1)) === 0 check for power of two?
Because subtracting 1 from a power of two flips the only '1' bit and all bits after it, so ANDing with original clears all bits, resulting in zero.
Click to reveal answer
beginner
What is a simple JavaScript function to check if a number is a power of two?
function isPowerOfTwo(n) { return n > 0 && (n & (n - 1)) === 0; }
Click to reveal answer
beginner
Can zero or negative numbers be powers of two?
No, powers of two are positive numbers only. Zero and negative numbers are not powers of two.
Click to reveal answer
Which of these numbers is a power of two?
A22
B18
C20
D16
✗ Incorrect
16 is 2 to the power 4, so it is a power of two.
What does the expression (n & (n - 1)) === 0 check for?
AIf n is prime
BIf n is even
CIf n is a power of two
DIf n is negative
✗ Incorrect
This expression is true only when n is a power of two.
What should be true about n before applying (n & (n - 1)) === 0 to check power of two?
An must be zero
Bn must be positive
Cn must be negative
Dn can be any integer
✗ Incorrect
Only positive numbers can be powers of two.
Which of these is NOT a power of two?
A3
B2
C1
D8
✗ Incorrect
3 is not a power of two.
What is the binary representation of a power of two?
AOnly one '1' bit and rest '0's
BAll bits are '1'
CAlternating '1' and '0' bits
DOnly '0' bits
✗ Incorrect
Powers of two have exactly one '1' bit in binary.
Explain how to check if a number is a power of two using bitwise operations.
Think about how subtracting 1 changes the bits of a power of two.
You got /4 concepts.
Write a simple JavaScript function to check if a number is a power of two and explain each step.
Start with checking if number is positive, then use bitwise trick.
You got /4 concepts.
Practice
(1/5)
1. Which of the following best describes a number that is a power of two?
easy
A. It is any even number greater than zero.
B. It is any number divisible by 3.
C. It can be written as 2 raised to an integer exponent (e.g., 2^3 = 8).
D. It is a number that ends with digit 2.
Solution
Step 1: Understand the definition of power of two
A power of two is a number that can be expressed as 2 raised to some integer exponent, like 2^0=1, 2^1=2, 2^2=4, etc.
Step 2: Compare options with the definition
Only It can be written as 2 raised to an integer exponent (e.g., 2^3 = 8). correctly describes this. Options A, C, and D do not match the definition.
Final Answer:
It can be written as 2 raised to an integer exponent (e.g., 2^3 = 8). -> Option C
Quick Check:
Power of two = 2^n [OK]
Hint: Power of two means 2 multiplied by itself n times [OK]
Common Mistakes:
Thinking all even numbers are powers of two
Confusing divisibility by 3 with powers of two
Assuming number ending with 2 is power of two
2. Which JavaScript expression correctly checks if a number n is a power of two using bitwise operations?
easy
A. n & (n - 1) === 0
B. n | (n - 1) === 0
C. n ^ (n - 1) === 0
D. n & (n + 1) === 0
Solution
Step 1: Understand bitwise check for power of two
A number that is a power of two has only one bit set. The expression n & (n - 1) clears the lowest set bit. If the result is zero, it means only one bit was set.
Step 2: Analyze each option
n & (n - 1) === 0 uses the correct expression. Options B, C, and D use incorrect bitwise operators or operands.
Final Answer:
n & (n - 1) === 0 -> Option A
Quick Check:
Bitwise AND with n-1 equals zero means power of two [OK]
Hint: Use n & (n-1) === 0 to check power of two [OK]
Common Mistakes:
Using OR or XOR instead of AND
Using n + 1 instead of n - 1
Not comparing result to zero
3. What is the output of the following JavaScript code?
function isPowerOfTwo(n) {
return n > 0 && (n & (n - 1)) === 0;
}
console.log(isPowerOfTwo(16));
console.log(isPowerOfTwo(18));
medium
A. false\nfalse
B. false\ntrue
C. true\ntrue
D. true\nfalse
Solution
Step 1: Evaluate isPowerOfTwo(16)
16 in binary is 10000. 16 & 15 (01111) = 0, so returns true.
Step 2: Evaluate isPowerOfTwo(18)
18 in binary is 10010. 18 & 17 (10001) = 16 (not zero), so returns false.
Final Answer:
true\nfalse -> Option D
Quick Check:
16 is power of two: true, 18 is not: false [OK]
Hint: Check bitwise AND with n-1 for zero to confirm power of two [OK]
Common Mistakes:
Assuming all even numbers return true
Forgetting to check n > 0
Misunderstanding bitwise AND result
4. Find the error in this JavaScript function that checks if a number is a power of two:
function checkPower(n) {
return (n & n - 1) === 0;
}
medium
A. Using bitwise OR instead of AND
B. Not checking if n is positive
C. Missing parentheses around (n & (n - 1))
D. Using subtraction instead of addition
Solution
Step 1: Test with edge case n=0
0 & (0 - 1) = 0 & (-1) = 0 === 0 returns true, but 0 is not a power of two.
Step 2: Analyze options
The function lacks n > 0 check. Not checking if n is positive identifies this. No OR used (B wrong), parentheses not required due to operator precedence (C wrong), subtraction is correct (D wrong).
Final Answer:
Not checking if n is positive -> Option B
Quick Check:
n > 0 && (n & (n-1)) === 0 [OK]
Hint: Include n > 0 check for power of two validation [OK]
Common Mistakes:
Ignoring operator precedence
Not checking if number is positive
Using wrong bitwise operators
5. You want to filter an array nums to keep only numbers that are powers of two. Which code snippet correctly does this in JavaScript?
hard
A. nums.filter(n => n > 0 && (n & (n - 1)) === 0)
B. nums.filter(n => n % 2 === 0)
C. nums.filter(n => n & (n + 1) === 0)
D. nums.filter(n => n > 0 && (n | (n - 1)) === 0)
Solution
Step 1: Understand filtering condition for power of two
We need to keep numbers greater than zero where n & (n - 1) === 0 is true.
Step 2: Evaluate each filter condition
nums.filter(n => n > 0 && (n & (n - 1)) === 0) correctly uses this condition. nums.filter(n => n % 2 === 0) filters even numbers, which is incorrect. Options C and D use wrong bitwise operations.
Final Answer:
nums.filter(n => n > 0 && (n & (n - 1)) === 0) -> Option A
Quick Check:
Filter with bitwise AND and n > 0 [OK]
Hint: Filter with n > 0 and n & (n-1) === 0 to find powers of two [OK]
Common Mistakes:
Filtering only even numbers
Using wrong bitwise operators
Not checking for positive numbers
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.