What does this bit-manipulating function do?

http://stackoverflow.com/questions/8637142/what-does-this-bit-manipulating-function-do

unsigned long ccNextPOT(unsigned long x){

    x = x - 1;
    x = x | (x >> 1);
    x = x | (x >> 2);
    x = x | (x >> 4);
    x = x | (x >> 8);
    x = x | (x >>16);
    return x + 1;
}

bit-manipulation


shareimprove this question

edited Dec 26 ‘11 at 16:42

Ondrej Tucny
13.8k22450

asked Dec 26 ‘11 at 15:45

guoxx
1007

 

3  

It works pretty fast. –  Sergio Tulentsev Dec 26 ‘11 at 15:46
    

i know it works well, but i want to know which algorithm it use. –  guoxx Dec 26 ‘11 at 15:51
2  

Have a look here. –  Howard Dec 26 ‘11 at 15:51

add a comment

2 Answers

activeoldestvotes


up vote2down vote

The OR and SHIFT statements fills with ones all bits of x to the right of most significant bit (up to 32 bits). Together with the pre-decrement and post-increment statements, this computes (as the function name suggets) the next power-of-two number, equal or greater than the given number (if x is greater than 0 and less than 2^32)


shareimprove this answer

answered Dec 26 ‘11 at 16:54

leonbloy
30.5k66491

 

    

The pre-decrement ensures inputs of zero and powers of two are mapped onto themselves. –  njuffa Dec 26 ‘11 at 17:41

add a comment


up vote0down vote

This function rounds x up to the next highest power of 2. It‘s exactly the code in here

unsigned int v; // compute the next highest power of 2 of 32-bit v

v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;

shareimprove this answer

edited Aug 1 ‘13 at 8:08

answered Jul 31 ‘13 at 13:45

L?u V?nh Phúc
4,75831332

 
add a comment
时间: 2024-11-25 20:21:34

What does this bit-manipulating function do?的相关文章

通过百度echarts实现数据图表展示功能

现在我们在工作中,在开发中都会或多或少的用到图表统计数据显示给用户.通过图表可以很直观的,直接的将数据呈现出来.这里我就介绍说一下利用百度开源的echarts图表技术实现的具体功能. 1.对于不太理解echarts是个怎样技术的开发者来说,可以到echarts官网进行学习了解,官网有详细的API文档和实例供大家参考学习. 2.以下是我在工作中实现整理出来的实例源码: 公用的支持js文件 echarts.js.echarts.min.js,还有其他的图表需要支持的js文件也可以到官网下载 echa

帮同学做的大一大作业:《我的家乡—郑州》

---恢复内容开始--- 最近在上海上学的一个高中同学让我帮忙,帮她做她们的计算机课程大作业. 由于关系不错我也不好意思拒绝就帮忙做了,因为这个学期刚刚开始接触HTML5和css,所以制作过程中有很多不懂的,而且由于HTML5是选修课,一星期只有一节,所以做这个花费了比较多的时间,这个网站是我制作的第一个网站,比较有纪念意义,所以发在博客上,作为纪念. 通过去做这个作业,我了解到很多课上学不到的东西.因为没有美工,从头到尾,都是我一个人在臆想,刚开始的时候,根本无从下手,我去参考别人做的家乡网站

《CS:APP》 chapter 2 Representing and Manipulating Information 笔记

Representing and Manipulating Information 首先,普及一下历史知识,原来十进制数都使用1000多年了...其实我真不知道...斐波拉契(Fibonacci)很屌的说(废话....) The familiar decimal, or base-10, representation has been in use for over 1000 years, having been developed in India, improved by Arab math

JQuery:$(...).ajaxSubmit is not a function

用jquery写表单回调的时候报的bug 正如stackover上说的 I'm guessing you don't have a jquery form plugin included. ajaxSubmit isn't a core jquery function, I believe. 意思就是说表单提交是单独的一个插件,没集成在jquery-版本号.min.js这个文件里,需要另外引入jquery-form.js,文件名自取.现在找到的官方最新的form插件代码: /*! * jQuer

a note of R software write Function

Functionals “To become significantly more reliable, code must become more transparent. In particular, nested conditions and loops must be viewed with great suspicion. Complicated control flows confuse programmers. Messy code often hides bugs.” — Bjar

Disposable microfluidic devices: fabrication, function, and application Gina S. Fiorini and Daniel T

Disposable microfluidic devices: fabrication, function, and application Gina S. Fiorini and Daniel T. Chiu BioTechniques 38:429-446 (March 2005) This review article describes recent developments in microfluidics, with special emphasis on disposable p

Adding New Functions to MySQL(User-Defined Function Interface UDF、Native Function)

catalog 1. How to Add New Functions to MySQL 2. Features of the User-Defined Function Interface 3. User-Defined Function 4. UDF Argument Processing 5. UDF Return Values and Error Handling 6. UDF Compiling and Installing 7. Adding a New Native Functio

安装python 第三方库遇到的安装问题 microsoft visual studio c++ 10.0 is required,Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?

问题一: microsoft visual studio c++ 10.0 is required 安装scrapy时候出现需要vc c++ 10,有时安装其他也会有. 解决方法:安装vc 2010,安装过2017无效,安装过程也不一样. 问题二: 安装好,出现Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed? 解决办法: 1.pip install wheel 2. 到http://

[c++] Inline Function

The inline functions are a C++ enhancement feature to increase the execution time of a program. Compiler replace the definition at compile time instead of referring function defination at runtime. NOTE - This is a suggestion to compiler to make the f

js instanceof Object Function

Object.Function是javascript中顶级的两个对象,同时也属于两个顶级的构造器,function Object(){}.function Function(){}.Object.Function为两个独立的预先创建的两个对象.new Object创建一个具有Object特性的新的一个对象,new Function创建一个具有Function特性的一个新对象. Object是一个对象,包含__proto__.prototype属性. Object.__proto__ = func