What does this bit-manipulating function do?
发布日期:2021-08-17 08:27:56 浏览次数:11 分类:技术文章

本文共 1206 字,大约阅读时间需要 4 分钟。

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;}
 
3  
It works pretty fast. –   
    
i know it works well, but i want to know which algorithm it use. –   
2  
Have a look . –   

2 Answers

2

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)

 
    
The pre-decrement ensures inputs of zero and powers of two are mapped onto themselves. –   
 
0

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

unsigned int v; // compute the next highest power of 2 of 32-bit vv--;v |= v >> 1;v |= v >> 2;v |= v >> 4;v |= v >> 8;v |= v >> 16;v++;
 

转载于:https://www.cnblogs.com/xuejinhui/p/4341764.html

转载地址:https://blog.csdn.net/weixin_30843605/article/details/95273165 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:习题二(7)
下一篇:iOS获取当前设备方向

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年03月31日 03时03分36秒