Exceeding the bit shift operator in C

the bit shift operator in C does not work, as I expect, which is, without a doubt, my misunderstanding, but can someone explain what is happening?

unsigned char in = 155;
unsigned char res;

res = (in << 6) >> 7;

should be the same as

res = in << 6;
res = res >> 7; // can also use res >>= 7;

but this is not so.

First result:

in  = 10011011
res = 01001101

Second (as expected):

in  = 10011011
res = 00000001

Thus, in the first case, he works with each change of the source data instead of working with the first shift, and then performs the second shift according to the first result. Ideas?

+4
source share
2 answers

int s. res, 8 , . , , .

+11

AFAIK, . . char.

, :

res = in << 6;
printf("%p %p \n",res,(in << 6));
+2

Source: https://habr.com/ru/post/1532418/


All Articles