sehoon joo
sehoon joo

Reputation: 13

Bit-shift behavior

uint16_t a = 0x00 << 8 + 0xB9;
printf("%d",a);

I'm expecting 185 as an output but I'm getting 0.

What is happening here?

Upvotes: 1

Views: 77

Answers (1)

Benjamin James Drury
Benjamin James Drury

Reputation: 2383

If you look at this link, you'll see that the order of precedence means that the addition is performed before the shift. Change your code to

uint16_t a = (0x00 << 8) + 0xB9;
printf("%d",a);

to see the desired behaviour.

Upvotes: 5

Related Questions