开发者

A "dynamic bitfield" in C

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 eas开发者_如何学JAVAily 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜