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
 

最新文章

  1. 修改TNSLSNR的端口
  2. MOTION-MATCHING IN UBISOFT’S FOR HONOR翻译
  3. 单身狗进化——求n!的位数
  4. C++基础知识之vector
  5. Windows技巧 - 右键菜单【在此处打开bash】
  6. (转)CSS3 @font-face
  7. 【转】简单的 Laravel 5 REST API
  8. hadoop问题锦集(一)-搭建集群环境时的常见问题
  9. 堆排序 Heap Sort
  10. phpcms V9静态判断会员登录状态的方法
  11. java和javax都是Java的API包,java是核心包,javax的x是extension的意思,也就是扩展包。
  12. IIS发布网站后局域网其他用户不能访问问题(转)
  13. Step one : 熟悉Unix/Linux Shell 常见命令行 (三)
  14. mysql具体语句示例
  15. 哈夫曼树压缩C#算法(huffman)
  16. MongoDB复制
  17. gcc8.2安装
  18. Java基础学习-Path环境变量的配置
  19. java 网络编程(五)Socket多线程上传文件
  20. 2.Dubbo2.5.3注册中心和监控中心部署

热门文章

  1. java实时监听日志写入kafka
  2. PHP.36-TP框架商城应用实例-后台12-商品管理-主分类添加、修改、搜索(连表查询)
  3. STM32无法使用IAR下载程序问题
  4. The Image Gallery Revisited
  5. gradle编译很慢解决方法
  6. 《Cracking the Coding Interview》——第16章:线程与锁——题目3
  7. 嗯,ACM按照这个一步一步来。
  8. Centos在VMware虚拟机上的网络配置一记
  9. 新兵易学,老兵易用----C++(C++11的学习整理---如何减少代码量,加强代码的可读性)
  10. redhat 安装python3