【发布时间】:2014-07-20 05:12:03
【问题描述】:
我需要一个 C 宏来获得大于给定数字的 2 的最小幂。
例如,FIRSTFREEBIT(0x16)(二进制1_0110)必须等于0x20。
我将把它用作:
#include <someheader.h> // defines SOME_X and SOME_Y
enum {
x = SOME_X,
y = SOME_Y,
z = FIRSTFREEBIT(x|y),
t = z << 1,
};
一个类似但略有不同的 SO 问题: Algorithm for finding the smallest power of two that's greater or equal to a given value
【问题讨论】:
-
应该(二进制 1_0000)返回 0x10 还是 0x20?基于“最小的幂二大于”,听起来你想要 0x20。
-
我想你的意思是
t = 1U << z -
@chux 是的,我希望 FIRSTFREEBIT(0x10) 为 0x20,因为我想要一个尚未使用的位。
标签: c bit-manipulation c-preprocessor