Discover a tiny trick that saves you from long, boring division checks!
Why Check if Number is Power of Two in DSA Java?
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 method is slow and boring. It's easy to make mistakes, especially with big numbers. You might lose track of divisions or forget to check if the number is exactly 1 at the end. This wastes time and causes errors.
Using a simple check with bits, we can quickly tell if a number is a power of two. This method uses the fact that powers of two have only one '1' bit in their binary form. It's fast, clean, and never makes mistakes.
boolean isPowerOfTwo(int number) {
while (number > 1) {
if (number % 2 != 0) return false;
number = number / 2;
}
return number == 1;
}boolean isPowerOfTwo(int number) {
return number > 0 && (number & (number - 1)) == 0;
}This lets you quickly and reliably check powers of two, enabling faster programs and easier problem solving.
In computer memory, sizes are often powers of two. 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 check is fast and simple.
Useful in memory management and optimization 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, e.g., 4 is 100.Step 2: Check other options
Odd numbers and divisibility by 3 do not define powers of two. Having all bits set to 1 is not a power of two.Final Answer:
It has exactly one bit set to 1 in its binary form. -> Option CQuick Check:
Power of two = single 1 bit [OK]
- Thinking all odd numbers are powers of two
- Confusing powers of two with numbers having all bits set
- Assuming divisibility by 3 means power of two
n is a power of two?Solution
Step 1: Understand bitwise check for power of two
The expressionn & (n - 1)clears the lowest set bit. For powers of two, this results in zero.Step 2: Analyze other options
Options A, C, and D use incorrect bitwise operations that do not correctly check power of two.Final Answer:
return (n & (n - 1)) == 0; -> Option BQuick Check:
Use n & (n-1) == 0 to check power of two [OK]
- Using OR or XOR instead of AND in bitwise check
- Checking n & (n + 1) which is incorrect
- Not considering zero or negative numbers
int n = 16; boolean result = (n & (n - 1)) == 0; System.out.println(result);
Solution
Step 1: Calculate n & (n - 1) for n = 16
16 in binary is 10000, 15 is 01111. Bitwise AND is 00000 (0).Step 2: Compare result to zero
Since 16 & 15 == 0, the expression is true, so result is true.Final Answer:
true -> Option AQuick Check:
16 is power of two, so output is true [OK]
- Confusing bitwise AND with OR
- Expecting false for powers of two
- Ignoring parentheses in expression
public static boolean isPowerOfTwo(int n) {
return (n & n - 1) == 0;
}Solution
Step 1: Identify issue with negative numbers
For n = Integer.MIN_VALUE (-2147483648), which is 0x80000000 in two's complement, n-1 = 0x7FFFFFFF (Integer.MAX_VALUE), and 0x80000000 & 0x7FFFFFFF == 0, so it incorrectly returns true.Step 2: Rule out other options
Return type is correct, it uses AND (&), and operator precedence correctly parses as n & (n - 1) (no missing parentheses needed).Final Answer:
Method does not handle negative numbers. -> Option AQuick Check:
n > 0 && (n & (n-1)) == 0 [OK]
- Not checking n > 0 before bitwise operation
- Ignoring negative input handling
- Confusing bitwise AND with OR
true if a given positive integer n is a power of two, and false otherwise. Which of the following implementations correctly handles all positive integers including 1, and excludes zero and negative numbers?Solution
Step 1: Check for positive number condition
Zero and negative numbers are not powers of two, son > 0is required.Step 2: Use bitwise check for power of two
The expression(n & (n - 1)) == 0correctly identifies powers of two including 1.Step 3: Analyze other options
return (n & (n - 1)) == 0; misses positive check, allowing zero. Options C and D use incorrect bitwise operations.Final Answer:
return n > 0 && (n & (n - 1)) == 0; -> Option DQuick Check:
Check positive and n & (n-1) == 0 [OK]
- Not excluding zero and negative numbers
- Using incorrect bitwise operators
- Missing parentheses around (n - 1)
Ready to prove it?
Pass the test and this pattern moves unknown
