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
beginner
Which bit pattern property helps to check if a number is a power of two?
A power of two has exactly one bit set to 1 in its binary form, and all other bits are 0.
Click to reveal answer
intermediate
Explain the expression: n & (n - 1) == 0 for checking power of two.
If n is a power of two, n & (n - 1) clears the only set bit, resulting in 0. For other numbers, it won't be zero.
Click to reveal answer
beginner
Why must we check that n > 0 when checking if n is a power of two?
Because zero or negative numbers are not powers of two, and the bit trick only works for positive integers.
Click to reveal answer
beginner
Write a simple Python function to check if a number is a power of two using bit manipulation.
def is_power_of_two(n):
return n > 0 and (n & (n - 1)) == 0
Click to reveal answer
Which of these numbers is NOT a power of two?
A18
B32
C16
D64
✗ Incorrect
18 is not a power of two because it cannot be expressed as 2 raised to an integer.
What does the expression n & (n - 1) do when n is a power of two?
AReturns n
BReturns 0
CReturns n-1
DReturns 1
✗ Incorrect
For powers of two, n & (n - 1) clears the only set bit, resulting in 0.
Why do we check if n > 0 before using the bit trick?
ABecause negative numbers are powers of two
BBecause it speeds up the calculation
CBecause zero and negatives are not powers of two
DBecause it changes the bit pattern
✗ Incorrect
Zero and negative numbers are not powers of two, so we exclude them first.
Which binary pattern represents a power of two?
ANo bits set
BMultiple bits set to 1
CAll bits set to 1
DExactly one bit set to 1
✗ Incorrect
A power of two has exactly one bit set to 1 in binary.
What will is_power_of_two(0) return using the bit trick method?
AFalse
BTrue
CError
DNone
✗ Incorrect
0 is not a power of two, so the function returns False.
Describe how to check if a number is a power of two using bit manipulation.
Think about how binary bits change when subtracting 1.
You got /3 concepts.
Write a Python function to check if a number is a power of two and explain each step.
Use the bit trick and explain why it works.
You got /4 concepts.
Practice
(1/5)
1. Which of the following numbers is a power of two?
easy
A. 16
B. 18
C. 20
D. 22
Solution
Step 1: Understand powers of two
Powers of two are numbers like 1, 2, 4, 8, 16, 32, etc., where each is 2 raised to an integer.
Step 2: Check each option
16 = 2^4, so it is a power of two. Others like 18, 20, 22 are not powers of two.
Final Answer:
16 -> Option A
Quick Check:
16 = 2^4 [OK]
Hint: Only numbers with one '1' bit in binary are powers of two [OK]
Common Mistakes:
Confusing even numbers with powers of two
Thinking any multiple of 2 is a power of two
2. Which of the following Python expressions correctly checks if n is a power of two?
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 AND check
For powers of two, binary form has only one '1'. n & (n-1) clears the lowest set bit. If result is 0, n is power of two.
Step 2: Analyze options
n & (n - 1) == 0 uses correct bitwise AND check. Others use OR, XOR, or addition which do not correctly check power of two.
Final Answer:
n & (n - 1) == 0 -> Option B
Quick Check:
Bitwise AND check is standard for 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
Forgetting to check n > 0 before this test
3. What is the output of the following code?
def is_power_of_two(n):
return n > 0 and (n & (n - 1)) == 0
print(is_power_of_two(32))
print(is_power_of_two(45))
medium
A. False\nFalse
B. False\nTrue
C. True\nFalse
D. True\nTrue
Solution
Step 1: Check 32
32 is 2^5, so n > 0 and n & (n-1) == 0 is True.
Step 2: Check 45
45 is not a power of two, so n & (n-1) != 0, returns False.
Final Answer:
True\nFalse -> Option C
Quick Check:
32 power of two = True, 45 not = False [OK]
Hint: Check output for known powers of two and non-powers [OK]
Common Mistakes:
Ignoring n > 0 condition
Assuming all even numbers are powers of two
4. Find the error in this code snippet that checks if a number is a power of two:
def check_power(n):
if n & (n - 1) == 0:
return True
else:
return False
print(check_power(0))
medium
A. Missing check for n > 0 before bitwise operation
B. No error, code works fine
C. Should use OR instead of AND
D. Return values should be strings 'True' or 'False'
Solution
Step 1: Analyze bitwise check
n & (n-1) == 0 works only if n > 0. For n=0, it returns True incorrectly.
Step 2: Identify missing condition
Code lacks n > 0 check, so 0 incorrectly returns True.
Final Answer:
Missing check for n > 0 before bitwise operation -> Option A
Quick Check:
Always check n > 0 before bitwise test [OK]
Hint: Always check n > 0 before bitwise power of two test [OK]
Common Mistakes:
Not handling zero or negative inputs
Using wrong bitwise operators
5. You want to filter a list to keep only numbers that are powers of two. Which code snippet correctly does this?
nums = [0, 1, 2, 3, 4, 5, 8, 16, 18]
A) result = [x for x in nums if x & (x - 1) == 0]
B) result = [x for x in nums if x > 0 and x & (x - 1) == 0]
C) result = [x for x in nums if x | (x - 1) == 0]
D) result = [x for x in nums if x > 0 or x & (x - 1) == 0]
hard
A. result = [x for x in nums if x & (x - 1) == 0]
B. result = [x for x in nums if x > 0 or x & (x - 1) == 0]
C. result = [x for x in nums if x | (x - 1) == 0]
D. result = [x for x in nums if x > 0 and x & (x - 1) == 0]
Solution
Step 1: Understand filtering condition
We must check x > 0 to avoid zero or negative numbers passing the test.
Step 2: Analyze each option
result = [x for x in nums if x > 0 and x & (x - 1) == 0] correctly uses x > 0 and bitwise check. result = [x for x in nums if x & (x - 1) == 0] misses x > 0, so 0 passes incorrectly. Options C and D use wrong operators or logic.
Final Answer:
result = [x for x in nums if x > 0 and x & (x - 1) == 0] -> Option D
Quick Check:
Filter with x > 0 and bitwise AND check [OK]
Hint: Filter with x > 0 and x & (x-1) == 0 for powers of two [OK]
Common Mistakes:
Not filtering out zero
Using OR instead of AND in condition
Using bitwise OR instead of AND
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.