最新最好的方法,从unsigned char类型。在我看来提取了一下,我觉得这个工作完全well`从缓冲区中提取位
int bit;
unsigned char buffer;
bit= 1 & (buffer>>3) //`if i want to extract the fourth bit
bit= 1 & (buffer>>7)//if i want to extract the 8 bit
最新最好的方法,从unsigned char类型。在我看来提取了一下,我觉得这个工作完全well`从缓冲区中提取位
int bit;
unsigned char buffer;
bit= 1 & (buffer>>3) //`if i want to extract the fourth bit
bit= 1 & (buffer>>7)//if i want to extract the 8 bit
如果你不关心该位是在最显著位置(例如,因为你需要它的布尔条件),你可以这样做:
if (buffer & (1<<3)) {
// ...
}
这可能是更快,因为constant folding:它只有一个操作在运行时,而不是两个。
我上面发布的代码将该位置于最低有效位 – 2012-02-22 00:21:54
@Jackwelch如果您需要最不重要位置的位,解决方案是最快的。 – dasblinkenlight 2012-02-22 01:51:45
是的,就是这样做的。你有问题吗? – 2012-02-22 00:05:54
除非你有充分的理由不这样做,否则你应该使用'bool bit;'。 'bool'是用于布尔变量的数据类型,其值可以是0或1. – 2012-02-22 00:13:57