[Leetcode] power 231.2 of

1, Title Description:
  Given an integer, write a function to determine whether it is a power of two.

  Example 1:

  Input: 1
  Output: true
  explanation: 1 = 20

  Example 2:

  Input: 16
  Output: true
  explanation: 24 = 16

  Example 3:

  Input: 218
  Output: false
2, thinking:
  Because the power of the word binary number is only one side of the 2 1, you can make the numbers continue to the right. After removal of the first one, at this time if the number is 0, a true, false otherwise
3, Code:  
class Solution {
    public boolean isPowerOfTwo(int n) {
        if(n<=0)
            return false;
        while(n-((n>>>1)<<1)!=1){
            n=n>>>1;
        }
        if ((n>>>1)!=0)
            return false;
        else
            return true;
    }
}
View Code
4, the focus of study: bit computing
5, reference materials: None
6, reference links: None
7, the title link: https: //leetcode-cn.com/problems/power-of-two/

Guess you like

Origin www.cnblogs.com/DoubleBarbecue/p/11332108.html