231. Power of Two

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

题目含义:判断一个数是否是2的n次方

 1     public boolean isPowerOfTwo(int n) {
 2         // int cnt = 0;
 3         // while (n > 0) {
 4         //     cnt += (n & 1);
 5         //     n >>= 1;
 6         // }
 7         // return cnt == 1;    
 8         
 9         // 方法二
10         if (n<=0) return false;
11         return (n&(n-1)) == 0;
12     }

 

posted @ 2017-10-20 20:28  daniel456  阅读(117)  评论(0编辑  收藏  举报