取最小的比n大的2的幂
#include <cstdio>inline bool is_power_of_2(int n)
{
return (n & (n-1)) == 0;
}inline int min_power_of_2_g(int n)
{
//return n |= n>>16, n |= n>>8, n |= n>>4, n |= n>>2, n |= n>>1, n + 1; 其实是一样的,显得短一些而已
return ((((n |= n>>16) |= n>>8) |= n>>4) |= n>>2) |= n>>1, n + 1;
}inline int min_power_of_2_eg(int n)
{
if (is_power_of_2(n))
return n;
return min_power_of_2_g(n);
}int main()
{
int n;
while(scanf("%d", &n) == 1)
{
n = min_power_of_2_eg(n);
printf("%x %dn", n, n);
}
return 0;
}
/**//*这里是vc6 release生成的代码:
mov ecx,dword ptr [esp] ; 取出n
mov eax,ecx
lea edx,[ecx-1] ;n-1
test ecx,edx ; n & (n-1)
je END1
sar eax,10h ; n |= n>>16
or eax,ecx
mov ecx,eax
sar ecx,8 ; n |= n>>8
or eax,ecx
mov edx,eax
sar edx,4 ; n |= n>>4
or eax,edx
mov ecx,eax
sar ecx,2 ; n |= n>>2
or eax,ecx
mov edx,eax
sar edx,1 ; n |= n>>1
or eax,edx
inc eax ; 最后加1
END1:
*/
inline int exponent_of_2(int n) // 直接取浮点数的指数部分
{
float t = n;
return ((*(unsigned int *)&t)>>23) - 127;
}
inline int min_power_of_2_g(int n)
{
if (n == 1)
return 1;
return 1 << exponent_of_2(n - 1) + 1;
}