JavaScript 检查给定的数是否为2的幂次
如果给定的数仅通过乘以2生成,则该数是2的幂。在本教程中,我们将学习如何检查给定的数是否是2的幂次。在这里,我们将看到5种不同的方法来检查给定的数是否是2的幂次。
使用Math.pow()方法
在JavaScript中,数字最多可以包含64位。因此,我们可以使用for循环和Math.pow()方法来找到2的1到64次幂。对于循环,我们可以将2的第i次幂与给定的数进行比较。如果匹配该数,我们返回true;否则,在循环结束时返回false。
语法
用户可以按照下面的语法使用for循环和Math.pow()方法来检查数是否可被2整除。
for () {
if (Math.pow(2, i) == num) {
return true;
}
}
步骤
- 步骤1 - 使用for循环,迭代i=1到i=64的数字。
-
步骤2 - 在循环中,使用Math.pow()方法获取2的第i次方。
-
步骤3 - 将2的第i次方与给定的数字进行比较。如果相符,返回true。
-
步骤4 - 如果for循环在没有返回true的情况下终止,返回false。
示例1
在下面的示例中,我们使用上述方法检查给定的数字是否为2的幂。用户可以观察到checkPowOf2()函数根据数字是否为2的幂返回true或false。
<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>
使用Math.log()方法
我们可以以2为底求一个数的对数。如果这个数是整数,那它就是2的幂。
语法
用户可以按照以下语法使用Math.log()方法来检查一个数是否是2的幂。
let log = Math.log(number) / Math.log(2);
let isInteger = parseInt(log) == log;
示例2
在以下示例中,首先,我们以2为底对数。之后,我们使用parseInt()方法提取出对数值整数部分,如果该整数部分与对数值相等,则函数返回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>
通过计算置位位确定
如果一个数字是2的幂,它只包含一个置位位。因此,我们可以逐个检查数字的每一位。如果我们找到第一个置位位,我们将isSetBit设置为true。之后,如果我们再次找到置位位,我们可以说该数字不是2的幂。
语法
用户可以按照以下语法通过计算置位位来确定一个数字是否是2的幂。
while (number) {
if (isSetBit) {
return false;
}
else if (number & 1 == 1) {
isSetBit = true;
}
number = number >> 1;
}
步骤
- 步骤1 - 在数字不等于零时使用 while 循环进行迭代。
-
步骤2 - 检查 ‘isSetBit’ 变量的值是否为真;返回假。
-
步骤3 - 如果 isSetBit 变量的值为假,并且当前位是一个设定位,则将 isSetBit 变量的值改为真。
-
步骤4 - 将数字右移一位。
示例3
在下面的示例中,我们使用 while 循环迭代数字,并检查数字的每一位。如果我们得到数字中的第二个设定位,则返回假。
<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>
使用‘&’运算符
如果一个数字是2的幂次方,它的最左位只包含1。如果我们从2的幂次方中减去1,这个数字的最左位就变为0,而其他位为1。因此,如果我们在n和n-1之间进行‘&’运算,对于所有等于2的幂次方的数字,它总是返回0。
语法
用户可以按照以下语法使用‘&’运算符来检查给定的数字是否是2的幂次方。
let isPowerOf2 = number && !(number & number - 1)
示例4
在下面的示例中,首先,在if语句中,我们检查数字是否不为零。然后,我们检查’n & n-1’是否等于零来决定这个数字是否是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>