简体   繁体   English

如何在 C/C++ 中读/写任意位

[英]How to read/write arbitrary bits in C/C++

Assuming I have a byte b with the binary value of 11111111假设我有一个二进制值为 11111111 的字节 b

How do I for example read a 3 bit integer value starting at the second bit or write a four bit integer value starting at the fifth bit?例如,如何读取从第二位开始的 3 位整数值或从第五位开始写入四位整数值?

Some 2+ years after I asked this question I'd like to explain it the way I'd want it explained back when I was still a complete newb and would be most beneficial to people who want to understand the process.在我问这个问题大约 2 年之后,我想以我还是一个完整的新手时想要的方式来解释它,这对想要了解这个过程的人来说是最有益的。

First of all, forget the "11111111" example value, which is not really all that suited for the visual explanation of the process.首先,忘记“11111111”示例值,它并不是真正适合过程的可视化解释。 So let the initial value be 10111011 (187 decimal) which will be a little more illustrative of the process.因此,让初始值为10111011 (十进制 187),这将更能说明该过程。

1 - how to read a 3 bit value starting from the second bit: 1 - 如何从第二位开始读取 3 位值:

    ___  <- those 3 bits
10111011 

The value is 101, or 5 in decimal, there are 2 possible ways to get it:该值为 101,即十进制的 5,有两种可能的获取方式:

  • mask and shift掩码和移位

In this approach, the needed bits are first masked with the value 00001110 (14 decimal) after which it is shifted in place:在这种方法中,需要的位首先用值00001110 (十进制 14)屏蔽,然后将其移位:

    ___
10111011 AND
00001110 =
00001010 >> 1 =
     ___
00000101

The expression for this would be: (value & 14) >> 1表达式为: (value & 14) >> 1

  • shift and mask转移和掩码

This approach is similar, but the order of operations is reversed, meaning the original value is shifted and then masked with 00000111 (7) to only leave the last 3 bits:这种方法是相似的,但操作顺序是相反的,这意味着原始值被移位,然后用00000111 (7) 屏蔽,只留下最后 3 位:

    ___
10111011 >> 1
     ___
01011101 AND
00000111
00000101

The expression for this would be: (value >> 1) & 7表达式为: (value >> 1) & 7

Both approaches involve the same amount of complexity, and therefore will not differ in performance.这两种方法涉及相同数量的复杂性,因此在性能上不会有差异。

2 - how to write a 3 bit value starting from the second bit: 2 - 如何从第二位开始写一个 3 位值:

In this case, the initial value is known, and when this is the case in code, you may be able to come up with a way to set the known value to another known value which uses less operations, but in reality this is rarely the case, most of the time the code will know neither the initial value, nor the one which is to be written.在这种情况下,初始值是已知的,当在代码中出现这种情况时,您也许可以想出一种方法将已知值设置为另一个使用较少操作的已知值,但实际上这很少是在大多数情况下,代码既不知道初始值,也不知道要编写的值。

This means that in order for the new value to be successfully "spliced" into byte, the target bits must be set to zero, after which the shifted value is "spliced" in place, which is the first step:这意味着为了将新值成功“拼接”成字节,目标位必须设置为零,之后将移位后的值“拼接”到位,这是第一步:

    ___ 
10111011 AND
11110001 (241) =
10110001 (masked original value)

The second step is to shift the value we want to write in the 3 bits, say we want to change that from 101 (5) to 110 (6)第二步是将我们要写入的值移入 3 位,假设我们要将其从 101 (5) 更改为 110 (6)

     ___
00000110 << 1 =
    ___
00001100 (shifted "splice" value)

The third and final step is to splice the masked original value with the shifted "splice" value:第三步也是最后一步是将屏蔽的原始值与移位后的“拼接”值拼接:

10110001 OR
00001100 =
    ___
10111101

The expression for the whole process would be: (value & 241) | (6 << 1)整个过程的表达式为: (value & 241) | (6 << 1) (value & 241) | (6 << 1)

Bonus - how to generate the read and write masks:奖励 - 如何生成读写掩码:

Naturally, using a binary to decimal converter is far from elegant, especially in the case of 32 and 64 bit containers - decimal values get crazy big.自然地,使用二进制到十进制转换器远非优雅,尤其是在 32 位和 64 位容器的情况下——十进制值变得非常大。 It is possible to easily generate the masks with expressions, which the compiler can efficiently resolve during compilation:可以使用表达式轻松生成掩码,编译器可以在编译期间有效地解析这些掩码:

  • read mask for "mask and shift": ((1 << fieldLength) - 1) << (fieldIndex - 1) , assuming that the index at the first bit is 1 (not zero)读取“掩码和移位”的掩码: ((1 << fieldLength) - 1) << (fieldIndex - 1) ,假设第一位的索引为 1(非零)
  • read mask for "shift and mask": (1 << fieldLength) - 1 (index does not play a role here since it is always shifted to the first bit读取“移位和掩码”的掩码: (1 << fieldLength) - 1 (索引在这里不起作用,因为它总是移到第一位
  • write mask: just invert the "mask and shift" mask expression with the ~ operator写入掩码:只需使用~运算符反转“掩码和移位”掩码表达式

How does it work (with the 3bit field beginning at the second bit from the examples above)?它是如何工作的(上面示例中的 3 位字段从第二位开始)?

00000001 << 3
00001000  - 1
00000111 << 1
00001110  ~ (read mask)
11110001    (write mask)

The same examples apply to wider integers and arbitrary bit width and position of the fields, with the shift and mask values varying accordingly.相同的示例适用于更宽的整数以及字段的任意位宽和位置,移位和掩码值相应变化。

Also note that the examples assume unsigned integer, which is what you want to use in order to use integers as portable bit-field alternative (regular bit-fields are in no way guaranteed by the standard to be portable), both left and right shift insert a padding 0, which is not the case with right shifting a signed integer.另请注意,这些示例假定无符号整数,这是您想要使用的整数作为可移植位域替代方案(标准无法保证常规位域是可移植的),左移和右移插入一个填充 0,这与右移有符号整数的情况不同。

Even easier:更简单:

Using this set of macros (but only in C++ since it relies on the generation of member functions):使用这组宏(但仅限于 C++,因为它依赖于成员函数的生成):

#define GETMASK(index, size) ((((size_t)1 << (size)) - 1) << (index))
#define READFROM(data, index, size) (((data) & GETMASK((index), (size))) >> (index))
#define WRITETO(data, index, size, value) ((data) = (((data) & (~GETMASK((index), (size)))) | (((value) << (index)) & (GETMASK((index), (size))))))
#define FIELD(data, name, index, size) \
  inline decltype(data) name() const { return READFROM(data, index, size); } \
  inline void set_##name(decltype(data) value) { WRITETO(data, index, size, value); }

You could go for something as simple as:你可以做一些简单的事情:

struct A {
  uint bitData;
  FIELD(bitData, one, 0, 1)
  FIELD(bitData, two, 1, 2)
};

And have the bit fields implemented as properties you can easily access:并将位字段实现为您可以轻松访问的属性:

A a;
a.set_two(3);
cout << a.two();

Replace decltype with gcc's typeof pre-C++11.用 gcc 的typeof pre-C++11 替换decltype

You need to shift and mask the value, so for example...您需要移动并屏蔽该值,例如...

If you want to read the first two bits, you just need to mask them off like so:如果你想读取前两位,你只需要像这样屏蔽掉它们:

int value = input & 0x3;

If you want to offset it you need to shift right N bits and then mask off the bits you want:如果你想偏移它,你需要右移 N 位,然后屏蔽掉你想要的位:

int value = (intput >> 1) & 0x3;

To read three bits like you asked in your question.像你在问题中问的那样阅读三位。

int value = (input >> 1) & 0x7;

just use this and feelfree:只需使用它并随意使用:

#define BitVal(data,y) ( (data>>y) & 1)      /** Return Data.Y value   **/
#define SetBit(data,y)    data |= (1 << y)    /** Set Data.Y   to 1    **/
#define ClearBit(data,y)  data &= ~(1 << y)   /** Clear Data.Y to 0    **/
#define TogleBit(data,y)     (data ^=BitVal(y))     /** Togle Data.Y  value  **/
#define Togle(data)   (data =~data )         /** Togle Data value     **/

for example:例如:

uint8_t number = 0x05; //0b00000101
uint8_t bit_2 = BitVal(number,2); // bit_2 = 1
uint8_t bit_1 = BitVal(number,1); // bit_1 = 0

SetBit(number,1); // number =  0x07 => 0b00000111
ClearBit(number,2); // number =0x03 => 0b0000011

You have to do a shift and mask (AND) operation.您必须执行移位和屏蔽 (AND) 操作。 Let b be any byte and p be the index (>= 0) of the bit from which you want to take n bits (>= 1).b为任意字节, p为要从中获取n位 (>= 1) 的位的索引 (>= 0)。

First you have to shift right b by p times:首先你必须将b右移p次:

x = b >> p;

Second you have to mask the result with n ones:其次,你必须用n个来掩盖结果:

mask = (1 << n) - 1;
y = x & mask;

You can put everything in a macro:您可以将所有内容放在一个宏中:

#define TAKE_N_BITS_FROM(b, p, n) ((b) >> (p)) & ((1 << (n)) - 1)

"How do I for example read a 3 bit integer value starting at the second bit?" “例如,我如何读取从第二位开始的 3 位整数值?”

int number = // whatever;
uint8_t val; // uint8_t is the smallest data type capable of holding 3 bits
val = (number & (1 << 2 | 1 << 3 | 1 << 4)) >> 2;

(I assumed that "second bit" is bit #2, ie the third bit really.) (我假设“第二位”是位#2,即实际上是第三位。)

To read bytes use std::bitset读取字节使用 std::bitset

const int bits_in_byte = 8;

char myChar = 's';
cout << bitset<sizeof(myChar) * bits_in_byte>(myChar);

To write you need to use bit-wise operators such as & ^ |要编写,您需要使用按位运算符,例如 & ^ | & << >>. & << >>。 make sure to learn what they do.确保了解他们的工作。

For example to have 00100100 you need to set the first bit to 1, and shift it with the << >> operators 5 times.例如,要获得 00100100,您需要将第一位设置为 1,并使用 << >> 运算符将其移位 5 次。 if you want to continue writing you just continue to set the first bit and shift it.如果你想继续写,你只需继续设置第一位并移动它。 it's very much like an old typewriter: you write, and shift the paper.它很像一台老式打字机:你打字,然后移动纸张。

For 00100100: set the first bit to 1, shift 5 times, set the first bit to 1, and shift 2 times:对于 00100100:将第一位设置为 1,移位 5 次,将第一位设置为 1,并移位 2 次:

const int bits_in_byte = 8;

char myChar = 0;
myChar = myChar | (0x1 << 5 | 0x1 << 2);
cout << bitset<sizeof(myChar) * bits_in_byte>(myChar);
int x = 0xFF;   //your number - 11111111

How do I for example read a 3 bit integer value starting at the second bit例如,我如何读取从第二位开始的 3 位整数值

int y = x & ( 0x7 << 2 ) // 0x7 is 111
                         // and you shift it 2 to the left

If you keep grabbing bits from your data, you might want to use a bitfield.如果您一直从数据中获取位,则可能需要使用位域。 You'll just have to set up a struct and load it with only ones and zeroes:您只需要设置一个结构并仅使用 1 和 0 加载它:

struct bitfield{
    unsigned int bit : 1
}
struct bitfield *bitstream;

then later on load it like this (replacing char with int or whatever data you are loading):然后稍后像这样加载它(用 int 或您正在加载的任何数据替换 char):

long int i;
int j, k;
unsigned char c, d;

bitstream=malloc(sizeof(struct bitfield)*charstreamlength*sizeof(char));
for (i=0; i<charstreamlength; i++){
    c=charstream[i];
    for(j=0; j < sizeof(char)*8; j++){
        d=c;
        d=d>>(sizeof(char)*8-j-1);
        d=d<<(sizeof(char)*8-1);
        k=d;
        if(k==0){
            bitstream[sizeof(char)*8*i + j].bit=0;
        }else{
            bitstream[sizeof(char)*8*i + j].bit=1;
        }
    }
}

Then access elements:然后访问元素:

bitstream[bitpointer].bit=...

or要么

...=bitstream[bitpointer].bit

All of this is assuming are working on i86/64, not arm, since arm can be big or little endian.所有这些都假设在 i86/64 上工作,而不是在 arm 上工作,因为 arm 可以是大端或小端。

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

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