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;
}
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

2 Answers

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)

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
 

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++;
answered Jul 31 '13 at 13:45
 

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

  1. C++ 风格与技术 FAQ(中文版)

    Bjarne Stroustrup 的 C++ 风格与技术 FAQ(中文版) 原作:Bjarne Stroustrup    翻译:Antigloss 译者的话:尽管我已非常用心,力求完美,但受水平所 ...

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

    现在我们在工作中,在开发中都会或多或少的用到图表统计数据显示给用户.通过图表可以很直观的,直接的将数据呈现出来.这里我就介绍说一下利用百度开源的echarts图表技术实现的具体功能. 1.对于不太理解 ...

  3. 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 ...

  4. 神经网络可以拟合任意函数的视觉证明A visual proof that neural nets can compute any function

    One of the most striking facts about neural networks is that they can compute any function at all. T ...

  5. a note of R software write Function

    Functionals “To become significantly more reliable, code must become more transparent. In particular ...

  6. jsp中出现onclick函数提示Cannot return from outside a function or method

    在使用Myeclipse10部署完项目后,原先不出错的项目,会有红色的叉叉,JSP页面会提示onclick函数错误 Cannot return from outside a function or m ...

  7. JavaScript function函数种类

    本篇主要介绍普通函数.匿名函数.闭包函数 目录 1. 普通函数:介绍普通函数的特性:同名覆盖.arguments对象.默认返回值等. 2. 匿名函数:介绍匿名函数的特性:变量匿名函数.无名称匿名函数. ...

  8. 在ubuntu16.10 PHP测试连接MySQL中出现Call to undefined function: mysql_connect()

    1.问题: 测试php7.0 链接mysql数据库的时候发生错误: Fatal error: Uncaught Error: Call to undefined function mysqli_con ...

  9. jquery中的$(document).ready(function() {});

    当文档载入时执行function函数里的代码, 这部分代码主要声明,页面加载后 "监听事件" 的方法.例如: $(document).ready( $("a") ...

  10. Function.prototype.toString 的使用技巧

    Function.prototype.toString这个原型方法可以帮助你获得函数的源代码, 比如: function hello ( msg ){ console.log("hello& ...

随机推荐

  1. Pandas 索引和切片

    Series和Datafram索引的原理一样,我们以Dataframe的索引为主来学习 列索引:df['列名'] (Series不存在列索引) 行索引:df.loc[].df.iloc[] 选择列 / ...

  2. Lighting System Design UVA - 11400 动态规划

    题目:题目链接 思路:简单的动态规划问题,先把灯泡按照电压从小到大排序.设s[i]为前i种灯泡的总数量(即L值之和),d[i]为灯 泡1-i的最小开销,则d[i] = min{d[j] + (s[i] ...

  3. 关于原生JS获取class,ID等属性的一些封装

    一.传统上获取是通过document.getElementById获取元素的ID属性,通过总结与学习总结一下获取元素class以及id属性的一些封装; 1.创建构造函数,这里面不需要多解释什么:(主要 ...

  4. div嵌套img高度不相同

    div中嵌套img,如果div里嵌套一个img元素且div的高度是由img的高度来撑开,那么div的高度总会比img的高度多3px. 可以明显看到div实际高度高出img高度3px.为了解决此问题,我 ...

  5. 基类VS接口

    该篇引用 CLR via C# 中的13.11节. 应该设计基类还是接口,这个问题不能一概而论,下面提供一些指导性原则: 1. IS_A关系(指属于,例如汽车属于交通工具) vs CAN_DO关系(指 ...

  6. 三 APPIUM GUI讲解(Windows版)

    本文本转自:http://www.cnblogs.com/sundalian/p/5629386.html APPIUM GUI讲解(Windows版)   Windows版本的APPIUM GUI有 ...

  7. HTML5标签学习笔记

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta http-equiv="con ...

  8. CentOS 7.5 部署蓝鲸运维平台

    环境准备 官方建议 准备至少3台 CentOS 7 以上操作系统的机器 最低配置:2核4G 建议配置: 4核12G 以上 部署前关闭待安装主机之间防火墙,保证蓝鲸主机之间通信无碍 部署前关闭SELin ...

  9. Unresolved defparam reference to 'read_aclr_synch' in dcfifo_component.read_aclr_synch

    问题: 今天用 questasim 仿真出现下面这个问题. Unresolved defparam reference to 'read_aclr_synch' in dcfifo_component ...

  10. ExtJS Ext.MessageBox.alert()弹出对话框详解

    Ext.MessageBox是一个工具类,他继承自Obiect对象,用来生成各种风格的信息提示对话框,Ext.Msg是该类的别名,使用Ext.MessageBox和用Ext.Msg效果是一样的,而后者 ...