I'm trying to exact a 3 bit value from a byte for example:
unsigned char r = ((253 << 5) >> 5);
Result value should be 5 but why does it returns 253?
Two possibilities:
Note that your result should be 5, not 3. What you want is:
const unsigned char c = 253 & 0x07 ; // (00000111)
Your bit mask is:
253 | 1 1 1 1 1 1 0 1 | F D
7 | 0 0 0 0 0 1 1 1 | 0 7
+++++++++++++++++++++++++++
5 | 0 0 0 0 0 1 0 1 | 0 5
Your expression is evaluated using ints. Change it to:
unsigned char r = ((unsigned char)(253 << 5) >> 5);
and you should get the desired result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With