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?
If you want to be generic,
And for endianness, see When to worry about endianness?.