Cracking the coding interview--Q5.4
来源:互联网 发布:晶晶改车软件下载 编辑:程序博客网 时间:2024/06/10 07:44
题目
原文:
Explain what the following code does: ((n & (n-1)) == 0).
译文:
解释下面代码的作用:((n&(n-1))==0)
解答
分析:首先该代码是一个位运算的式子,两个相邻的数作按位与运算得到0,而当两个二进制的各位不同时,作按位与运算才会等于0,所以只有当n为2的整数次幕时,比它小1的数的二进制的各位才会与它的二进制的各位不同,所以此式子的作用是判断n是否是2的整数次幕,但需要注意此式子并不严谨,当n=0时,此式为true,但0并不是2的整数次幕,所以上式子改为:
(n > 0) && ((n & (n-1)) == 0)
更多(n&(n-1))妙用请点击:http://blog.csdn.net/navyifanr/article/details/19496459
---EOF---
0 0
- Cracking the coding interview--Q5.4
- Cracking the coding interview--Q5
- Cracking the coding interview--Q5.1
- Cracking the coding interview--Q5.2
- Cracking the coding interview--Q5.3
- Cracking the coding interview--Q5.5
- Cracking the coding interview--Q5.6
- Cracking the coding interview--Q5.7
- Cracking the coding interview--Q5.1
- Cracking the coding interview--Q5.2
- Cracking the coding interview--Q5.3
- Cracking the coding interview--Q5.5
- Cracking the coding interview--Q5.6
- Cracking the coding interview--Q5.7
- Cracking the Coding Interview
- 《Cracking the coding interview》
- Cracking The Coding Interview
- Cracking the coding interview
- IBM Rational Appscan Part 1
- google搜索技巧
- The declared package does not match the expected package
- Eclipse error: “The import XXX cannot be resolved”
- xss攻击汇总--转
- Cracking the coding interview--Q5.4
- qobject_cast<QPushButton *>( sender() ) 用法介绍
- 找个地盘
- cpu使用率 linux
- 状态压缩(1) Hdu 4539 郑厂长系列故事——排兵布阵
- HashMap和Hashtable的区别
- 众筹,帮创业者跨过“死亡之谷”
- 正则表达式使用小结
- 宝很多宝贝是公开打8折、甚至