简体   繁体   English

从缓冲区中提取一点

[英]extracting a bit from a buffer

Whats the best way to extract a bit from an unsigned char .In my opinion ,I think this works perfectly well` 从未签名的char中提取一点的最佳方法是什么。在我看来,我认为这非常有效。

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 you do not care for the bit to be in the least significant position (eg because you need it for a boolean condition) you can do this: 如果您不希望位位于最低有效位(例如,因为布尔条件需要使用该位),则可以执行以下操作:

if (buffer & (1<<3)) {
    // ...
}

This may be faster because of constant folding : it is only one operation at runtime instead of two. 由于不断折叠,这可能会更快:在运行时它仅是一项操作,而不是两项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM