Java [Leetcode 231]Power of Two

题目描述:

Given an integer, write a function to determine if it is a power of two.

解题思路:

判断方法主要依据2的N次幂的特点:仅有首位为1,其余各位都为0.

代码如下:

public class Solution {
    public boolean isPowerOfTwo(int n) {
        return (n > 0) && ( n & (n - 1)) == 0;
    }
}

  

posted @ 2016-01-05 14:48  scottwang  阅读(269)  评论(0编辑  收藏  举报