Chinaunix首页 | 论坛 | 博客
  • 博客访问: 689129
  • 博文数量: 113
  • 博客积分: 7070
  • 博客等级: 少将
  • 技术积分: 930
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-02 21:03
文章分类

全部博文(113)

文章存档

2012年(2)

2011年(10)

2010年(19)

2009年(39)

2008年(20)

2007年(23)

我的朋友

分类: 嵌入式

2011-06-10 11:21:50

简单来讲,如果X为2的N次幂,则(X&(X-1)) == 0成立。

*************************************************

The binary representation of integers makes it possible to apply a very fast test to determine whether a given positive integer x is a power of two:

    x is a power of two \Leftrightarrow (x & (x − 1)) equals zero.

where & is a bitwise logical AND operator.

Examples:
−1 = 1...111...1         
−1 = 1...111...111...1
x = 0...010...0         
y = 0...010...010...0
x−1 = 0...001...1         
y−1 = 0...010...001...1
x & (x−1) = 0...000...0         
y & (y−1) = 0...010...000...0

Note that zero is incorrectly considered a power of two by this test.


阅读(1729) | 评论(0) | 转发(0) |
0

上一篇:Nginx

下一篇:MTK生成LIB

给主人留下些什么吧!~~