JS Basic
JS Interview Programs
- JS Interview Programs
- JS Abundant Number
- JS Amicable Number
- JS Armstrong Number
- JS Average of N Numbers
- JS Automorphic Number
- JS Biggest of three numbers
- JS Binary to Decimal
- JS Common Divisors
- JS Composite Number
- JS Condense a Number
- JS Cube Number
- JS Decimal to Binary
- JS Decimal to Octal
- JS Disarium Number
- JS Even Number
- JS Evil Number
- JS Factorial of a Number
- JS Fibonacci Series
- JS GCD
- JS Happy Number
- JS Harshad Number
- JS LCM
- JS Leap Year
- JS Magic Number
- JS Matrix Addition
- JS Matrix Division
- JS Matrix Multiplication
- JS Matrix Subtraction
- JS Matrix Transpose
- JS Maximum Value of an Array
- JS Minimum Value of an Array
- JS Multiplication Table
- JS Natural Number
- JS Number Combination
- JS Odd Number
- JS Palindrome Number
- JS Pascalβs Triangle
- JS Perfect Number
- JS Perfect Square
- JS Power of 2
- JS Power of 3
- JS Pronic Number
- JS Prime Factor
- JS Prime Number
- JS Smith Number
- JS Strong Number
- JS Sum of Array
- JS Sum of Digits
- JS Swap Two Numbers
- JS Triangular Number
JavaScript Program to Check Power of 2
Photo Credit to CodeToFun
π Introduction
In the realm of programming, efficiency and optimization are key considerations. One common optimization involves determining whether a given number is a power of 2.
Checking if a number is a power of 2 is a fundamental operation with applications in various domains.
In this tutorial, we'll explore a JavaScript program that efficiently checks whether a given number is a power of 2.
π Example
Let's take a look at the JavaScript code that accomplishes this functionality.
// Function to check if a number is a power of 2
function isPowerOfTwo(num) {
// A number is a power of 2 if and only if it has a single set bit.
// Using bitwise AND operation to check this condition.
return num > 0 && (num & (num - 1)) === 0;
}
// Driver program
// Replace this value with your desired number
const number = 16;
// Check if the number is a power of 2
if (isPowerOfTwo(number)) {
console.log(`${number} is a power of 2.`);
} else {
console.log(`${number} is not a power of 2.`);
}
π» Testing the Program
To test the program with different numbers, simply replace the value of number in the code.
16 is a power of 2.
Run the script to see whether the given number is a power of 2.
π§ How the Program Works
- The program defines a function isPowerOfTwo that takes an integer as input and returns true if the number is a power of 2 and false otherwise.
- Inside the function, it uses a bitwise AND operation to check if the number has a single set bit, which is a characteristic of power-of-2 numbers.
- The driver program demonstrates the usage of the isPowerOfTwo function by checking if a given number is a power of 2.
π Between the Given Range
Let's dive into the javascript code that performs the check for powers of 2 in the range 1 to 20.
// Function to check if a number is a power of 2
function isPowerOf2(num) {
return num !== 0 && (num & (num - 1)) === 0;
}
// Function to list powers of 2 in a given range
function listPowersOf2(rangeStart, rangeEnd) {
let powers = [];
for (let i = rangeStart; i <= rangeEnd; i++) {
if (isPowerOf2(i)) {
powers.push(i);
}
}
return powers;
}
// Range for powers of 2
const rangeStart = 1;
const rangeEnd = 20;
// Call the function to list powers of 2 in the specified range
const powersList = listPowersOf2(rangeStart, rangeEnd);
// Output the result
console.log(`Power of 2 in the range ${rangeStart} to ${rangeEnd}:`);
console.log(powersList.join(' '));
π» Testing the Program
Power of 2 in the range 1 to 20: 1 2 4 8 16
You can test the program with the fixed range by running the script. The output will display the powers of 2 within the specified range.
π§ How the Program Works
- The program defines a function isPowerOf2 that checks whether a given number is a power of 2.
- It then defines a function listPowersOf2 that iterates through a specified range and collects the numbers that are powers of 2.
- The program sets a range from 1 to 20 and calls the function to list the powers of 2 within that range.
- The results are output in the specified format.
π§ Understanding the Concept of Power of 2
A number is considered a power of 2 if and only if it has a single set bit.
For example, 2, 4, 8, and 16 are powers of 2 because their binary representations have only one bit set.
Understanding this concept is crucial for efficient programming and algorithm design.
π’ Optimizing the Program
The provided program is a straightforward implementation. However, there are more advanced techniques for checking if a number is a power of 2, such as using logarithmic operations or counting the number of set bits. Depending on your specific use case, you might explore these optimizations.
Feel free to incorporate and modify this code as needed for your specific requirements. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (JavaScript Program to Check Power of 2), please comment here. I will help you immediately.