x&x-1的问题?
2012-07-04 13:49
69 查看
1. 判断一个整数是否是2的N次方?
2.统计一个整数的二进制中1的个数
x&x-1表示把数字x对应的二进制的最右边的1变为0。
bool foo(int n) { if(n == 0) return true; if(n < 0) return false; return !(n&n-1); }
2.统计一个整数的二进制中1的个数
int foo(int n) { int nCount = 0; while(n) { nCount++; n = n&(n-1); } return nCount; }
x&x-1表示把数字x对应的二进制的最右边的1变为0。
相关文章推荐
- [问题记录.dotnet]取网卡信息报错"找不到"-WMI - Not found
- rpm出现"error: %preun( ) scriptlet failed, exit status 1问题
- 关于文本框显示&nbsp问题
- UNIX环境高级编程中的11章程序11-2编译问题---undefined reference to `pthread_create'
- ROR部署到Heroku出现Application Error和code=H10 desc="App crashed“问题
- 使用dsoframer控件出现"Unable to display the inactive document. Click here to reactivate the document."的问题
- insmod问题: init_module xxxxx.ko' failed (Operation not permitted)
- SDUT 1157-小鼠迷宫问题(BFS&DFS)
- 【转载】两军问题与Paxos算法 & 动画讲解Paxos算法
- Linux-->问题解决-->/dev/mapper/VolGroup-lv_root:
- 初级问题:<meta http-equiv=...>定认语言,设定页面跳转时间
- Centos&nbsp;6.5&nbsp;机群搭建及问题解决
- internet explorer cannot display image picture from google group?我的问题就这么解决了View > Encoding >
- undefined reference to `sqrt' 问题
- 如何解决"应用程序无法启动,因为应用程序的并行配置不正确"问题
- AFNetworking网络状态判断和网络请求The network connection was lost."问题
- Cloning into '.git'...遇到的问题
- 如何解决取消win7登录时显示“ctrl+alt+delete"的问题
- Xcode升级到5.1& iOS升级到iOS7.1问题:Undefined symbols for architecture x86_64
- "struts中文问题","struts国际化问题"??终极解决方案