最新最好的方法,從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