An offset to the right or left will be filled with 0 respectively, to the left or right of the byte. After the switch, you need an OR with the correct value to get what you expect.
x = (x >> 1); x = x | ( 0x80 );
Here I am OR ing with a byte of 10000000 , which is 0x80 , which leads to 0xC7 .
Having made it shorter, it will be as follows:
x = (x >> 1) | (unsigned char)0x80;
source share