I have a hex number 0x8F
(10001111 in binary). I want to right shift that value, so the new one would be 0xC7
(11000111). I tried with:
unsigned char x = 0x8F;
x=x>>1;
but instead of 0xC7 I got 0x47? Any ideas on how to do this?
Right shift on an unsigned quantity will make new zeros to enter, not ones.
Note that right shift is not a right rotation. To do that you need
x = (x >> 1) | (x << 7)
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