Discover a simple trick that turns a slow, boring check into a lightning-fast operation!
Why Check if Number is Power of Two in DSA Javascript?
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 to see if you end up with 1.
Doing this division repeatedly for many numbers is slow and tiring. It's easy to make mistakes, especially if the numbers are very large. Also, manually checking wastes a lot of time when you have thousands of numbers.
Using a simple trick with bits, computers can quickly check if a number is a power of two without dividing. This method is fast and accurate, even for very large numbers, saving time and effort.
function isPowerOfTwo(number) {
while (number > 1) {
if (number % 2 !== 0) return false;
number = number / 2;
}
return number === 1;
}function isPowerOfTwo(number) {
return number > 0 && (number & (number - 1)) === 0;
}This lets you instantly know if a number is a power of two, enabling faster programs and smarter decisions in tasks like memory allocation and game development.
When a game engine needs to allocate memory for textures, it often requires sizes that are powers of two for better performance. Quickly checking this helps the game run smoothly.
Manual division to check powers of two is slow and error-prone.
Bitwise operations provide a fast and reliable solution.
This technique is useful in programming tasks involving memory and optimization.
Practice
Solution
Step 1: Understand the definition of power of two
A power of two is a number that can be expressed as 2 raised to some integer exponent, like 2^0=1, 2^1=2, 2^2=4, etc.Step 2: Compare options with the definition
Only It can be written as 2 raised to an integer exponent (e.g., 2^3 = 8). correctly describes this. Options A, C, and D do not match the definition.Final Answer:
It can be written as 2 raised to an integer exponent (e.g., 2^3 = 8). -> Option CQuick Check:
Power of two = 2^n [OK]
- Thinking all even numbers are powers of two
- Confusing divisibility by 3 with powers of two
- Assuming number ending with 2 is power of two
n is a power of two using bitwise operations?Solution
Step 1: Understand bitwise check for power of two
A number that is a power of two has only one bit set. The expressionn & (n - 1)clears the lowest set bit. If the result is zero, it means only one bit was set.Step 2: Analyze each option
n & (n - 1) === 0 uses the correct expression. Options B, C, and D use incorrect bitwise operators or operands.Final Answer:
n & (n - 1) === 0 -> Option AQuick Check:
Bitwise AND with n-1 equals zero means power of two [OK]
- Using OR or XOR instead of AND
- Using n + 1 instead of n - 1
- Not comparing result to zero
function isPowerOfTwo(n) {
return n > 0 && (n & (n - 1)) === 0;
}
console.log(isPowerOfTwo(16));
console.log(isPowerOfTwo(18));Solution
Step 1: Evaluate isPowerOfTwo(16)
16 in binary is 10000. 16 & 15 (01111) = 0, so returns true.Step 2: Evaluate isPowerOfTwo(18)
18 in binary is 10010. 18 & 17 (10001) = 16 (not zero), so returns false.Final Answer:
true\nfalse -> Option DQuick Check:
16 is power of two: true, 18 is not: false [OK]
- Assuming all even numbers return true
- Forgetting to check n > 0
- Misunderstanding bitwise AND result
function checkPower(n) {
return (n & n - 1) === 0;
}Solution
Step 1: Test with edge case n=0
0 & (0 - 1) = 0 & (-1) = 0 === 0 returns true, but 0 is not a power of two.Step 2: Analyze options
The function lacks n > 0 check. Not checking if n is positive identifies this. No OR used (B wrong), parentheses not required due to operator precedence (C wrong), subtraction is correct (D wrong).Final Answer:
Not checking if n is positive -> Option BQuick Check:
n > 0 && (n & (n-1)) === 0 [OK]
- Ignoring operator precedence
- Not checking if number is positive
- Using wrong bitwise operators
nums to keep only numbers that are powers of two. Which code snippet correctly does this in JavaScript?Solution
Step 1: Understand filtering condition for power of two
We need to keep numbers greater than zero wheren & (n - 1) === 0is true.Step 2: Evaluate each filter condition
nums.filter(n => n > 0 && (n & (n - 1)) === 0) correctly uses this condition. nums.filter(n => n % 2 === 0) filters even numbers, which is incorrect. Options C and D use wrong bitwise operations.Final Answer:
nums.filter(n => n > 0 && (n & (n - 1)) === 0) -> Option AQuick Check:
Filter with bitwise AND and n > 0 [OK]
- Filtering only even numbers
- Using wrong bitwise operators
- Not checking for positive numbers
Ready to prove it?
Pass the test and this pattern moves unknown
