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 some integer exponent, like 1, 2, 4, 8, 16, etc.
Click to reveal answer
intermediate
Which bitwise operation can quickly check if a number is a power of two?
Using (n & (n - 1)) == 0 checks if n is a power of two, because powers of two have only one bit set.
Click to reveal answer
intermediate
Why must we check if the number is greater than zero before using (n & (n - 1))?
Because zero and negative numbers are not powers of two, and (n & (n - 1)) == 0 is true for zero, so we exclude zero by checking n > 0.
Click to reveal answer
beginner
What is the output of the function if the input number is 16?
The function returns true because 16 is 2^4, a power of two.
Click to reveal answer
beginner
Write the C code snippet to check if an integer n is a power of two.
int isPowerOfTwo(int n) {
return n > 0 && (n & (n - 1)) == 0;
}
Click to reveal answer
Which of these numbers is NOT a power of two?
A10
B8
C16
D32
✗ Incorrect
10 is not a power of two because it cannot be written as 2 raised to an integer.
What does the expression (n & (n - 1)) == 0 check for?
AIf n is zero
BIf n is a power of two
CIf n is negative
DIf n is odd
✗ Incorrect
This expression is true only when n is a power of two (and n > 0).
Why do we check n > 0 before applying (n & (n - 1)) == 0?
ABecause it improves performance
BBecause negative numbers are powers of two
CBecause zero also satisfies (n & (n - 1)) == 0 but is not a power of two
DBecause it checks if n is odd
✗ Incorrect
Zero satisfies the bitwise condition but is not a power of two, so we exclude it by checking n > 0.
What is the output of isPowerOfTwo(1)?
ATrue
BFalse
CError
DDepends on compiler
✗ Incorrect
1 is 2^0, so it is a power of two and the function returns true.
Which of these is a correct way to check if n is a power of two in C?
Areturn n % 2 == 0;
Breturn n & (n - 1);
Creturn n == 0 || n == 1;
Dreturn n > 0 && (n & (n - 1)) == 0;
✗ Incorrect
Option D correctly checks if n is positive and has only one bit set.
Explain how to check if a number is a power of two using bitwise operations.
Think about how powers of two look in binary.
You got /4 concepts.
Write a simple C function to check if a number is a power of two and explain why it works.
Use (n & (n - 1)) == 0 condition.
You got /4 concepts.
Practice
(1/5)
1. Which of the following statements correctly describes a number that is a power of two?
easy
A. It has exactly one bit set to 1 in its binary form.
B. It is always an odd number.
C. It is divisible by 3.
D. It has all bits set to 1 in its binary form.
Solution
Step 1: Understand binary representation of powers of two
A power of two in binary has exactly one '1' bit and the rest are '0's. For example, 4 is 100 in binary.
Step 2: Analyze the options
It has exactly one bit set to 1 in its binary form. matches the binary property of powers of two. Options B, C, and D do not describe powers of two correctly.
Final Answer:
It has exactly one bit set to 1 in its binary form. -> Option A
Quick Check:
Power of two = one '1' bit [OK]
Hint: Power of two has only one '1' bit in binary [OK]
Common Mistakes:
Thinking powers of two are always odd
Assuming powers of two have multiple '1' bits
Confusing divisibility rules
2. Which of the following C code snippets correctly checks if an integer n is a power of two?
easy
A. if (n & (n - 1)) { /* not power of two */ } else { /* power of two */ }
B. if (n | (n - 1)) { /* power of two */ } else { /* not power of two */ }
C. if (n & (n + 1)) { /* power of two */ } else { /* not power of two */ }
D. if (n ^ (n - 1)) { /* power of two */ } else { /* not power of two */ }
Solution
Step 1: Understand bitwise check for power of two
The expression n & (n - 1) is zero only if n is a power of two.
Step 2: Analyze each option
if (n & (n - 1)) { /* not power of two */ } else { /* power of two */ } uses the correct expression. Options A, C, and D use incorrect bitwise operations that do not correctly check for power of two.
Final Answer:
if (n & (n - 1)) { /* not power of two */ } else { /* power of two */ } -> Option A
Quick Check:
n & (n-1) == 0 means power of two [OK]
Hint: Use n & (n-1) == 0 to check power of two [OK]
Common Mistakes:
Using OR instead of AND in bitwise check
Using n + 1 instead of n - 1
Confusing XOR with AND
3. What is the output of the following C code snippet?
int n = 16;
if (n & (n - 1))
printf("No\n");
else
printf("Yes\n");
medium
A. Runtime error
B. No
C. Compilation error
D. Yes
Solution
Step 1: Calculate n & (n - 1) for n = 16
16 in binary is 10000. 15 is 01111. Bitwise AND: 10000 & 01111 = 00000 (0).
Step 2: Check condition and output
Since the result is 0, the else block executes, printing "Yes".
Final Answer:
Yes -> Option D
Quick Check:
16 & 15 = 0 means power of two [OK]
Hint: If n & (n-1) is zero, print Yes [OK]
Common Mistakes:
Miscomputing bitwise AND
Confusing output branches
Assuming non-zero means power of two
4. Identify the error in the following C code that checks if n is a power of two:
int n = 10;
if (n & n - 1 == 0)
printf("Power of two\n");
else
printf("Not power of two\n");
medium
A. No error, code is correct
B. Missing parentheses around (n - 1) in bitwise operation
C. Incorrect variable initialization
D. Using bitwise OR instead of AND
Solution
Step 1: Analyze operator precedence in condition
In C, == has higher precedence than &, so expression is evaluated as n & ((n - 1) == 0) which is incorrect.
Step 2: Correct the expression with parentheses
Use (n & (n - 1)) == 0 to ensure correct evaluation.
Final Answer:
Missing parentheses around (n - 1) in bitwise operation -> Option B
Quick Check:
Use parentheses to fix precedence [OK]
Hint: Add parentheses: (n & (n - 1)) == 0 [OK]
Common Mistakes:
Ignoring operator precedence
Not using parentheses in bitwise expressions
Assuming code compiles correctly without fix
5. You want to write a function in C that returns 1 if a number n is a power of two and 0 otherwise. Which of the following implementations correctly handles the case when n is zero or negative?
hard
A. int isPowerOfTwo(int n) { return n >= 0 && (n & (n + 1)) == 0; }
B. int isPowerOfTwo(int n) { return (n & (n - 1)) == 0; }
C. int isPowerOfTwo(int n) { return n > 0 && (n & (n - 1)) == 0; }
D. int isPowerOfTwo(int n) { return n != 0 || (n & (n - 1)) == 0; }
Solution
Step 1: Consider zero and negative inputs
Zero and negative numbers are not powers of two, so the function must exclude them.
Step 2: Analyze each function
int isPowerOfTwo(int n) { return n > 0 && (n & (n - 1)) == 0; } checks if n > 0 and then applies the bitwise check correctly. int isPowerOfTwo(int n) { return (n & (n - 1)) == 0; } misses the positive check. Options A, B, and D use incorrect logic or operators.
Final Answer:
int isPowerOfTwo(int n) { return n > 0 && (n & (n - 1)) == 0; } -> Option C
Quick Check:
Check n > 0 before bitwise test [OK]
Hint: Check n > 0 before bitwise test to avoid errors [OK]
Common Mistakes:
Not handling zero or negative inputs
Using wrong bitwise operators
Using OR instead of AND in conditions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.