A “dynamic bitfield” in C

隐身守侯 提交于 2019-12-08 05:23:01

问题


In this question, assume all integers are unsigned for simplicity.

Suppose I would like to write 2 functions, pack and unpack, which let you pack integers of smaller width into, say, a 64-bit integer. However, the location and width of the integers is given at runtime, so I can't use C bitfields.

Quickest is to explain with an example. For simplicity, I'll illustrate with 8-bit integers:

             * *
bit #    8 7 6 5 4 3 2 1
myint    0 1 1 0 0 0 1 1

Suppose I want to "unpack" at location 5, an integer of width 2. These are the two bits marked with an asterisk. The result of that operation should be 0b01. Similarly, If I unpack at location 2, of width 6, I would get 0b100011.

I can write the unpack function easily with a bitshift-left followed by a bitshift right.

But I can't think of a clear way to write an equivalent "pack" function, which will do the opposite.

Say given an integer 0b11, packing it into myint (from above) at location 5 and width 2 would yield

             * *
bit #    8 7 6 5 4 3 2 1
myint    0 1 1 1 0 0 1 1

Best I came up with involves a lot of concatinating bit-strings with OR, << and >>. Before I implement and test it, maybe somebody sees a clever quick solution?


回答1:


Off the top of my head, untested.

int pack(int oldPackedInteger, int bitOffset, int bitCount, int value) {
    int mask = (1 << bitCount) -1;
    mask <<= bitOffset;
    oldPackedInteger &= ~mask;
    oldPackedInteger |= value << bitOffset;
    return oldPackedInteger;
}

In your example:

int value = 0x63;
value = pack(value, 4, 2, 0x3);

To write the value "3" at an offset of 4 (with two bits available) when 0x63 is the current value.



来源:https://stackoverflow.com/questions/5149616/a-dynamic-bitfield-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!