Eray Xx
Eray Xx

Reputation: 313

Does integer promotion occur after explicit cast?

char port = 0xa5;

I wonder how the expression below evaluated according to standard.

(uint8_t) (~port) >> 4

Does left argument first converted to uint8_t and then promoted to int or vice versa?

Upvotes: 1

Views: 80

Answers (1)

mrks
mrks

Reputation: 8343

Casts have higher precedence than bit shifts, so the cast should be executed first.

I also found this resource which explains the need for the cast:

In this compliant solution, the bitwise complement of port is converted back to 8 bits. Consequently, result_8 is assigned the expected value of 0x0aU.

uint8_t port = 0x5a;
uint8_t result_8 = (uint8_t) (~port) >> 4;

Note that the complement is converted to 8 bits, not the final result of the entire expression.

Upvotes: 1

Related Questions