Power of Two

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

2的幂数二进制只有一个1

1 bool isPowerOfTwo(int n) {
2     if(n <= 0)
3         return 0;
4     return (n & (n-1)) == 0;
5 }

 

posted @ 2016-05-11 22:16  米开朗菠萝  阅读(103)  评论(0编辑  收藏  举报