Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bit operators with unsigned characters

Tags:

c

unsigned char x = 93;
unsigned char a = x << 4;
printf("a = %d\n", a);

I do understand how bit operators work but I don't understand the binary representation of x.

How is a = 208?

like image 906
foo Avatar asked Apr 26 '26 17:04

foo


2 Answers

93 = 01011101

Shift that left 4 bits and it leaves the following (only 8 bits in your result):

11010000 = 208
like image 155
Mark Wilkins Avatar answered Apr 29 '26 08:04

Mark Wilkins


x = 93 = 0x5D = 0101 1101
         << 4 = 1101 0000

1101 0000 in decimal is 208.

like image 38
Jerry Coffin Avatar answered Apr 29 '26 07:04

Jerry Coffin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!