No, powers of two are positive integers only. Zero and negative numbers are not powers of two.
Click to reveal answer
Which of these numbers is a power of two?
A16
B18
C20
D22
✗ Incorrect
16 is 2 raised to the 4th power, so it is a power of two.
What does the expression n & (n-1) == 0 check for?
AIf n is zero
BIf n is even
CIf n is a power of two
DIf n is odd
✗ Incorrect
This expression is true only when n is a power of two.
What should be true about n for the check n > 0 && (n & (n-1)) == 0 to work correctly?
An must be positive
Bn can be any integer
Cn must be negative
Dn must be zero
✗ Incorrect
The number must be positive because powers of two are positive integers.
What is the output of isPowerOfTwo(0) in the Go function?
Aundefined
Btrue
Cerror
Dfalse
✗ Incorrect
Zero is not a power of two, so the function returns false.
Which bitwise operator is used in the power of two check?
A| (OR)
B& (AND)
C^ (XOR)
D~ (NOT)
✗ Incorrect
The AND operator (&) is used to check if only one bit is set.
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 Go function to determine if a number is a power of two and explain each step.
Use n > 0 && (n & (n-1)) == 0 in your function.
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
The statement "It has exactly one bit set to 1 in its binary form" matches the binary property of powers of two. The other options do not: powers of two (except 1) are even, not divisible by 3, and do not have all bits set to 1.
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
Confusing powers of two with numbers divisible by 3
Assuming all bits are set to 1
2. Which of the following Go code snippets correctly checks if an integer n is a power of two?
easy
A. if n & (n + 1) == 0 { /* power of two */ }
B. if n | (n - 1) == 0 { /* power of two */ }
C. if n & (n - 1) == 0 { /* power of two */ }
D. if n ^ (n - 1) == 0 { /* power of two */ }
Solution
Step 1: Understand bitwise check for power of two
The expression n & (n - 1) == 0 is true only if n is a power of two (and n > 0).
Step 2: Analyze each option
The snippet if n & (n - 1) == 0 { /* power of two */ } uses the correct bitwise AND operation. The others use incorrect bitwise operations that do not correctly check for power of two.
Final Answer:
if n & (n - 1) == 0 { /* power of two */ } -> Option C
Quick Check:
Use n & (n-1) == 0 to check power of two [OK]
Hint: Use n & (n-1) == 0 to check power of two [OK]
Common Mistakes:
Using bitwise OR instead of AND
Using n + 1 instead of n - 1
Using XOR which does not work here
3. What is the output of the following Go code?
package main
import "fmt"
func main() {
n := 16
if n > 0 && (n & (n - 1)) == 0 {
fmt.Println("Power of Two")
} else {
fmt.Println("Not Power of Two")
}
}
medium
A. Runtime Error
B. Power of Two
C. Compilation Error
D. Not Power of Two
Solution
Step 1: Evaluate the condition for n = 16
16 in binary is 10000. 16 - 1 = 15 (binary 01111). Bitwise AND: 10000 & 01111 = 0.
Step 2: Check the if condition
Since n > 0 and bitwise AND is 0, the condition is true, so it prints "Power of Two".
Final Answer:
Power of Two -> Option B
Quick Check:
16 & 15 == 0 means power of two [OK]
Hint: Check if n & (n-1) == 0 and n > 0 [OK]
Common Mistakes:
Ignoring n > 0 check
Misunderstanding bitwise AND result
Assuming all even numbers are powers of two
4. The following Go code is intended to check if n is a power of two. What is the error?
A. The function should return an integer, not bool.
B. The bitwise AND operator is used incorrectly.
C. The parentheses around n - 1 are unnecessary and cause error.
D. It does not handle the case when n is zero or negative.
Solution
Step 1: Understand the function logic
The expression n & (n - 1) == 0 returns true for powers of two and also for zero.
Step 2: Identify missing check
Zero and negative numbers are not powers of two, but this function returns true for zero. It lacks a check for n > 0.
Final Answer:
It does not handle the case when n is zero or negative. -> Option D
Quick Check:
Check n > 0 before bitwise test [OK]
Hint: Always check n > 0 before bitwise power of two test [OK]
Common Mistakes:
Not checking for n > 0
Misusing bitwise operators
Assuming parentheses cause error
5. You want to write a Go function that returns true if a number n is a power of two, but also handles zero and negative inputs correctly. Which of the following implementations is correct?
hard
A. func isPowerOfTwo(n int) bool {
return n > 0 && (n & (n - 1)) == 0
}
B. func isPowerOfTwo(n int) bool {
return n >= 0 && (n | (n - 1)) == 0
}
C. func isPowerOfTwo(n int) bool {
return n != 0 && (n & (n + 1)) == 0
}