Discover the magic trick that turns slow division into instant power-of-two checks!
Why Check if Number is Power of Two in DSA Go?
Imagine you have a big list of numbers and you want to find out which ones are powers of two, like 1, 2, 4, 8, 16, and so on. Doing this by hand means dividing each number by 2 again and again until you reach 1 or a number that is not divisible by 2.
This manual way is slow and boring, especially if the list is very long. It's easy to make mistakes counting divisions or forgetting to check all numbers. It wastes time and can cause errors.
Using a simple trick with bits, computers can quickly check if a number is a power of two without dividing repeatedly. This method is fast, clean, and reliable, saving time and avoiding mistakes.
func isPowerOfTwo(number int) bool {
if number < 1 {
return false
}
for number%2 == 0 {
number = number / 2
}
return number == 1
}func isPowerOfTwo(number int) bool {
return number > 0 && (number&(number-1)) == 0
}This lets you instantly check if a number is a power of two, enabling faster programs and easier problem solving.
In computer memory, sizes are often powers of two. Quickly checking if a size fits this rule helps optimize storage and speed.
Manual division to check powers of two is slow and error-prone.
Bitwise tricks provide a fast and simple solution.
This method improves performance and reliability in many computing tasks.
Practice
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 AQuick Check:
Power of two = one '1' bit [OK]
- Thinking powers of two are always odd
- Confusing powers of two with numbers divisible by 3
- Assuming all bits are set to 1
n is a power of two?Solution
Step 1: Understand bitwise check for power of two
The expressionn & (n - 1) == 0is true only ifnis a power of two (and n > 0).Step 2: Analyze each option
The snippetif 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 CQuick Check:
Use n & (n-1) == 0 to check power of two [OK]
- Using bitwise OR instead of AND
- Using n + 1 instead of n - 1
- Using XOR which does not work here
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")
}
}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 BQuick Check:
16 & 15 == 0 means power of two [OK]
- Ignoring n > 0 check
- Misunderstanding bitwise AND result
- Assuming all even numbers are powers of two
n is a power of two. What is the error?
func isPowerOfTwo(n int) bool {
return n & (n - 1) == 0
}Solution
Step 1: Understand the function logic
The expressionn & (n - 1) == 0returns 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 DQuick Check:
Check n > 0 before bitwise test [OK]
- Not checking for n > 0
- Misusing bitwise operators
- Assuming parentheses cause error
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?Solution
Step 1: Check for positive input
The function must ensurenis greater than zero to exclude zero and negatives.Step 2: Use correct bitwise condition
The condition(n & (n - 1)) == 0correctly identifies powers of two.Step 3: Analyze options
The implementationfunc isPowerOfTwo(n int) bool { return n > 0 && (n & (n - 1)) == 0 }correctly combines both checks. The others use incorrect operators or conditions.Final Answer:
func isPowerOfTwo(n int) bool { return n > 0 && (n & (n - 1)) == 0 } -> Option AQuick Check:
Check n > 0 and n & (n-1) == 0 [OK]
- Using bitwise OR instead of AND
- Not checking n > 0
- Using wrong bitwise expressions
Ready to prove it?
Pass the test and this pattern moves unknown
