JS how to determine whether a number is 2 n power

Source: Internet
Author: User
Tags bitwise

Yesterday to interview, the interviewer asked an interview question, said how to determine whether a number is 2 of the n power, I did not know that 2 of the power of the n is what (embarrassing big?? ), it's okay to explain it to me. Finally go home on the internet to look up data, collated a method.

Method One

How to determine whether a number is a 2 of the N power, the simple method of judging is this number num directly except 2, if the remainder is 0, then NUM/2 2, and then determine whether the remainder is 0, is the words continue to press the previous step, until the last for the num=1.

For example: 2 2%2=0 (2/2) =1 is

4 4%2=0 (4/2)%2=0 (4/2/2) =1 Yes

6 6%2=0 (6/2)%2=1 not

7 7%2=1 not

24 24%2=0 (24/2)%2=0 (24/2/2)%2=0 (24/2/2/2)%2=1 not

Code implementation:

functionCheck (num) {if(num! = 1){         while(num! = 1){            if(num%2 = = 0) {num= NUM/2; }Else{                return false; }        }        return true; }Else{        return true; }}

The results are as follows:

Method Two

The binary method can be used to determine whether a number num is 2 n power, the law is known, as long as the power of 2, it is bound to be the highest bit 1, the remaining 0, when num-1, the highest bit is 0, the remaining 1.

Bitwise AND Operation: 1&1=1 0&1=0 0&0=0 1&0=0

2---> 3---> 11

4---> 6---> 110

8---> 7---> 111

Example: Binary 1000 8-1 of binary 0111 bitwise AND Operation 1000&0111 8--and 0000 so 8 is the n power of 2.

Binary 1001 of 9 binary 1000 Bitwise AND operation 1001&1000---> 1000 so 9 is not a power of 2.

Binary 11000 of 24 binary 10111 bitwise AND operation 11000&10111---> 10000 so 24 is not a power of 2.

You can write the binary of Num by num.tostring (2).

Code implementation:

function Check (num) {    return (num > 0) && ((Num & (num-1)) = = 0);}

The results are as follows:

The other 1 is also 2 of the 0 power.

There are other conditions not written, this method is to determine whether a number is 2 n power, and did not write this number is not an integer, you can add the condition to judge.

JS how to determine whether a number is 2 n power

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.