I searched online and the common solution for returning x
bits from start
seems to be:
mask = ((1 << x) - 1 ) << start
then, mask & value
.
However, I'm confused on how this works still.
If I have a number 0101 1100
and I want to return the two bits from positions 5 and 6 (11
)
mask = ((1<<2)-1) << 5
1<<2 = 0000 0100
, and subtracting 1 yields 0000 0011
then, shifting 5 is 0110 0000
If I take 0110 0000 & 0101 1100
(the original), I get 0100 0000
This is not the answer I want, so what am I doing wrong?