JavaScript Program to Check whether a Given Number is Power of 2



To check whether a given number is power of 2 in JavaScript, we can check if the number is generated using multiplying 2's only. We will be discussing 5 different approaches to check whether a given number is a power of 2.

In this article we are having two numbers, our task is to check whether a given number is power of 2 in JavaScript. Users must be familiar with JavaScript Math functions, loops, binary representation and bitwise operators.

Approaches to Check if a Number is Power of 2

Here is a list of approaches to check whether a given number is power of 2 in JavaScript which we will be discussing in this article with stepwise explaination and complete example codes.

Using Brute Force Method

To check whether a given number is power of 2 in JavaScript we have used brute force approach where we keep dividing the given number by 2.

  • We have declared two variables, storing the numbers in number1 and number2 and defined a function check() to check if numbers are power of 2.
  • Then we have used if statement that returns false if number is less than 1. The while loop first checks if the number is divisible by 2 using modulus operator.
  • If the number is not divisible by 2, then if statement returns false representing number is not power of 2. Then number is again divided by 2. This process keeps repeating till we get 1.
  • The 1 represents the number is power of 2 and returns true and the output is displayed in web console using console.log().

Example

Here is a complete example code implementing above mentioned steps to check whether a given number is power of 2 in JavaScript using brute force approach.

let number1 = 256;
let number2 = 100;
function check(n) {
   if (n  1) {
      if (n % 2 !== 0) {
         return false;
      }
      n = n / 2;
   }
   return true;
}
console.log("The " + number1 + " is a power of 2 : " 
            + check(number1));  
console.log("The " + number2 + " is a power of 2 : " 
            + check(number2));

Using Math.pow() method

In this approach to check whether a given number is power of 2 in JavaScript we have used Math.pow() method. In JavaScript since number can contain 64 bits most so, we have used for loop with Math.pow() method to find the 1 to 64 power of the 2.

  • We have declared two variables storing the numbers in number1 and number2 and defined a function check() to check if numbers are power of 2.
  • Use for loop and make iteration for i=1 to i=64 numbers.
  • In the for loop, use the Math.pow() method to get the ith power of 2.
  • Compare the ith power of the 2 with the number. If it matches, return true.
  • Return false if for loop terminates without returning true.
  • The result is then displayed in web console using console.log().

Example

Here is a complete example code implementing above mentioned steps to check whether a given number is power of 2 in JavaScript using Math.pow() method.

let num1 = 342434535;
let num2 = 2048;
function check(num) {
   for (let i = 0; i 


Using Math.log() method

In this approach we have used Math.log() method to check whether a given number is power of 2 in JavaScript. We can take a logarithm of the number on base 2. If the number is an integer, it is a power of 2.

  • We have declared two variables storing the numbers in number1 and number2 and defined a function check() to check if numbers are power of 2.
  • We have taken a logarithm of the number on the base of 2. After that, we use the parseInt() method to extract the integer from the logarithmic value. If it is the same as the logarithmic value, the function returns true otherwise returns false.
  • The result is then displayed in web console using console.log().

Example

Here is a complete example code implementing above mentioned steps to check whether a given number is power of 2 in JavaScript using Math.log() method.

let number1 = 1024;
let number2 = 3454;
function check(number) {
    let log = Math.log(number) / Math.log(2);
    if (parseInt(log) == log) {
       return true;
    }
    return false;
}
console.log("The " + number1 + " is a power of 2 : " 
            + check(number1));
console.log("The " + number2 + " is a power of 2 : " 
            + check(number2));

By Counting the Set Bits

In this approach to check whether a given number is power of 2 in JavaScript we will be using counting the set bits methods.

  • We have declared two variables storing the numbers in number1 and number2 and defined a function check() to check if numbers are power of 2.
  • Initially we have set the isSetBit value to false i.e 0.
  • Make iteration using a while loop while the number is not equal to zero. First we check isSetBit, if it is true then the function returns false, meaning the number is not power of 2.
  • 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 and right shift the number by 1.
  • The result is then displayed in web console using console.log().

Example 1

If the number is a power of 2, it contains only a set bit (1). So, we can check every bit of the number one by one. If we get the first set bit, we set isSetBit equal to true. After that, if we get set bit again, we can say that number is not the power of 2.

Input: 
Let number = 8
Binary representation: 1000

number = 8 => LSB = 0
=> Right shift = number = 4(100)
number = 4 => LSB = 0
=> Right shift = number = 2(10)
number = 2 => LSB = 0
=> Right shift = number = 1(1)
number = 1 => LSB = 1 => isSetBit = true (1)
Output:  true

Input: 
Let number = 5
Binary representation: 101

number = 5 => LSB = 1 => isSetBit = true (1)
Right shift = number = 2(1)
number = 2 => LSB = 1 
Here, isSetBit is already true, function return false.
=>5 is not power of 2

Example 2

Here is a complete example code implementing above mentioned steps to check whether a given number is power of 2 in JavaScript by counting set bits.

let number1 = 2048;
let number2 = 87907;
function check(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;
}
console.log("The " + number1 + " is a power of 2 : "
   + check(number1));
console.log("The " + number2 + " is a power of 2 : "
   + check(number2));

Using AND Operator

To check whether a given number is power of 2 in JavaScript we have used AND operator.

  • We have declared two variables storing the numbers in number1 and number2 and defined a function check() to check if numbers are power of 2.
  • In function check(), we check if the number is positive, and number & number - 1 is 0. If it fulfill both the conditions, then function returns true indicating number is power of 2.
  • The result is then displayed in web console using console.log().

Example 1

If the number is a power of 2, it contains only 1 in the leftmost bit. If we subtract 1 from the power of 2, the number contains 0 in the leftmost bit and 1 in other bits. So, if we do the '&' operation between n and n-1, it always returns zero for all numbers equal to the power of 2.

Input:
let number = 4 = 100
number - 1 = 3 = 011

number AND number - 1 = 000
=> 4 is power of 2
Output: true

Input:
let number = 7 = 111
number - 1 = 6 = 110

number AND number - 1 = 110(non-zero)
=> 7 is not power of 2
Output: false

Example 2

Here is a complete example code implementing above mentioned steps to check whether a given number is power of 2 in JavaScript using bitwise AND operator.

let number1 = 8192;
let number2 = 409540;
function check(number) {
   if (number && !(number & number - 1)) {
      return true;
   }
   return false;
}
console.log("The " + number1 + " is a power of 2 : "
   + check(number1));
console.log("The " + number2 + " is a power of 2 : "
   + check(number2));

Conclusion

In this article, we have understood 5 different approaches to check whether a given number is power of 2 in JavaScript, which are: using brute force approach, Math.pow() method, Math.log() method, by counting set bit and bitwwise AND operator. Out of all the approaches, using AND operator is the most effcient approach.

Practice and learn from a wide range of JavaScript examples, including event handling, form validation, and advanced techniques. Interactive code snippets for hands-on learning.
Updated on: 2024-12-03T14:37:26+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements