If a given number is generated by multiplying only 2, then the number is a power of 2. In this tutorial, we will learn to check if a given number is a power of 2. Here we will look at 5 different ways to check if a given number is a power of 2.
In JavaScript, numbers can contain up to 64 bits. Therefore, we can use a for loop and the Math.pow() method to find 2 raised to the power of 1 to 64. For loop, we can compare 2 raised to the ith power with a number. If it matches a number, we return true; otherwise, we return false when the loop terminates.
Users can use the for loop and Math.pow() method according to the following syntax to check whether the number is divisible by 2.
for () { if (Math.pow(2, i) == num) { return true; } }
Step 1 - Use a for loop and iterate over the numbers i=1 to i=64.
Step 2 - In the for loop, use the Math.pow() method to get 2 raised to the i power.
Step 3 - Compare 2 raised to the i power with the number. If there is a match, returns true.
Step 4 - If the for loop terminates without returning true, return false.
In the following example, we use the above method to check if a given number is a power of 2. In the output, the user can observe that the checkPowOf2() function returns true or false based on whether the number is a power or not.
<html> <body> <h3> Using the <i> Math.pow() method </i> to check whether the given number is a power of 2 or not </h3> <div id="output"> </div> <script> let output = document.getElementById('output'); let num1 = 342434535; let num2 = 2048; function checkPowOf2(num) { for (let i = 0; i < 64; i++) { if (Math.pow(2, i) == num) { return true; } } return false; } output.innerHTML += "The " + num1 + " is a power of 2 :- " + checkPowOf2(num1) + " <br> "; output.innerHTML += "The " + num2 + " is a power of 2 :- " + checkPowOf2(num2) + " <br> "; </script> </body> </html>
We can take the logarithm of a number with base 2. If the number is an integer, it is a power of 2.
Users can use the Math.log() method according to the following syntax to check whether the number is a power of 2.
let log = Math.log(number) / Math.log(2); let isInteger = parseInt(log) == log;
In the following example, we first take the base 2 logarithm of the number, and then use the parseInt() method to extract the integer from the logarithm. If it matches the logarithm, the function returns true.
<html> <body> <h3> Using the <i> Math.log() method </i> to check whether the given number is a power of 2 or not </h3> <div id="output"> </div> <script> let output = document.getElementById('output'); let number1 = 1024; let number2 = 3454; function checkPowOf2(number) { // get a log of the number on base 2 let log = Math.log(number) / Math.log(2); // If the log is an integer, return true. if (parseInt(log) == log) { return true; } return false; } output.innerHTML += "The " + number1 + " is a power of 2 :- " + checkPowOf2(number1) + " <br> "; output.innerHTML += "The " + number2 + " is a power of 2 :- " + checkPowOf2(number2) + " <br> "; </script> </body> </html>
If the number is a power of 2, it contains only one set bit. So, we can check each digit of the number one by one. If we get the first set bit, we set isSetBit to true. Afterwards, if we set the bit again, we can say that the number is not a power of 2.
Users can follow the syntax below to determine whether the number is a power of 2 by calculating the number of digits set.
while (number) { if (isSetBit) { return false; } else if (number & 1 == 1) { isSetBit = true; } number = number >> 1; }
Step 1 - Use a while loop to iterate when the number is not equal to 0.
Step 2 - Check if the value of the "isSetBit" variable is true; return an error.
Step 3 - If the value of the isSetBit variable is false and the current bit is a set bit, change the value of the isSetBit variable to true.
< /里>Step 4 - Move the number to the right by 1.
In the example below, we use a while loop to iterate over the numbers and check each digit of the number. If we get the second set bit in the number, we will return false.
<html> <body> <h3> Counting the <i> set bits </i> to check whether the given number is a power of 2 or not </h3> <div id="output"> </div> <script> let output = document.getElementById('output'); let number1 = 2048 * 2 * 2 * 2; let number2 = 87907; function checkPowOf2(number) { let isSetBit = false; if (number) { while (number) { if (isSetBit) { return false; } else if (number & 1 == 1) { isSetBit = true; } number = number >> 1; } return true; } return false; } output.innerHTML += "The " + number1 + " is a power of 2 :- " + checkPowOf2(number1) + " <br> "; output.innerHTML += "The " + number2 + " is a power of 2 :- " + checkPowOf2(number2) + " <br> "; </script> </body> </html>
If the number is a power of 2, the leftmost bit contains only 1. If we subtract 1 from a power of 2, the leftmost bit of the number contains 0 and the other bits contain 1. So if we do the "&" operation between n and n-1, it will always return zero for all numbers equal to the power of 2.
Users can use the '&' operator according to the following syntax to check whether the given number is a power of 2.
let isPowerOf2 = number && !(number & number - 1)
In the following example, we first check if the number in the if statement is not zero. We then check if "n & n-1" is equal to 0 to see if the number is a power of 2.
<html> <body> <h3> Using the <i> & operator </i> to check whether the given number is a power of 2 or not </h3> <div id="output"> </div> <script> let output = document.getElementById('output'); let number1 = 1024 * 2 * 2 * 2; let number2 = 409540; function checkPowOf2(number) { if (number && !(number & number - 1)) { return true; } return false; } output.innerHTML += "The " + number1 + " is a power of 2 :- " + checkPowOf2(number1) + " <br> "; output.innerHTML += "The " + number2 + " is a power of 2 :- " + checkPowOf2(number2) + " <br> "; </script> </body> </html>
The above is the detailed content of JavaScript program to check if a given number is a power of 2. For more information, please follow other related articles on the PHP Chinese website!