码迷,mamicode.com
首页 > 其他好文 > 详细

231. Power of Two

时间:2017-10-20 21:51:19      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:func   turn   ==   rmi   color   eterm   false   function   amp   

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     }

 

231. Power of Two

标签:func   turn   ==   rmi   color   eterm   false   function   amp   

原文地址:http://www.cnblogs.com/wzj4858/p/7701380.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!