Jay
Jay

Reputation: 24915

Extracting the value represented by a particular set of bits in a number

How to extract the value represented by a particular set of bits in a given number i.e. if bits 11,12 & 13 are 1,1,0 then the value should be 6.

What is the most efficient way of doing the same? Also, it should be generic. I should be able to give start and end bit positions and should be able to extract the value represented by the bits present between the start and end positions.

Ex: 00000000 00000000 01100000 00011111

For the above number, considering 0th bit is from the right end, if I give this number, 0 as starting position and 2 as end position, then I should get the value 7.

Also, how do we take care of endianness also for the above problem?

Upvotes: 2

Views: 366

Answers (1)

kennytm
kennytm

Reputation: 523784

six = (value >> 12) & 7;

If you want to be generic,

inline unsigned extract_continuous_bits(unsigned value, int start, int end) {
    unsigned mask = (~0u) >> (CHAR_BIT*sizeof(value) - end - 1);
    return (value & mask) >> start;
}

assert(extract_continuous_bits(0x601f, 12, 14) == 6));
assert(extract_continuous_bits(0x601f, 0, 2) == 7));
assert(extract_continuous_bits(0xf0f0f0f0, 0, 31) == 0xf0f0f0f0));
assert(extract_continuous_bits(0x12345678, 16, 31) == 0x1234));
assert(extract_continuous_bits(0x12345678, 0, 15) == 0x5678));

And for endianness, see When to worry about endianness?.

Upvotes: 6

Related Questions