简体   繁体   English

C位操作

[英]C bit manipulation

I have to accomplish bit manipulation. 我必须完成位操作。

My task is to iterate through many one byte values, and pull out certain amount of bits from each byte (sometimes odd, sometimes even) and be able to combine all those together. 我的任务是遍历许多一个字节的值,并从每个字节中取出一定数量的位(有时是奇数,有时甚至是偶数),并能够将所有这些组合在一起。 Is there a good way to accomplish this? 有一个好的方法可以做到这一点吗?

Here's a better explanation. 这是一个更好的解释。 My goal is to allow me to take certain bits from one byte and combine them with bits from another byte. 我的目标是允许我从一个字节中提取某些位,并将其与另一个字节中的位组合。

So for example, combine the first 3 bits from 0xE1, with the last 5 bits from 0xA1. 因此,例如,将0xE1的前3位与0xA1的后5位合并。

Extracting bits from integral values can be done with the bitwise and and shift operations. 从整数值中提取位可以通过按位移位操作来完成。

unsigned int c = 23;

c & 0xF  // extract the lowest 4 bits, 0xF is binary 1111
c & 0x7F // extract the lowest 7 bits, 0x7F is binary 1111111
(c >> 4) & 0x3 // extract 2 bits starting at bit 4 (0 indexed). 

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

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