In C, all integer computations are promoted to at least int
1. So
x = (date << 7) >> 12
= (5225 << 7 /* result as int */) >> 12
= 668800 >> 12
= 163
after the computation is complete, we truncate the result back to an unsigned short
to get 163.
In the second case, the y
forced the result to be truncated to be an unsigned short
, so
y = (unsigned short) (date << 7)
= (unsigned short) 668800
= 13440
z = y >> 12
= 3
1: C11 §6.5.7/3: "The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand."; §6.3.1.1/2: "If an int
can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int
; otherwise, it is converted to an unsigned int
. These are called the integer promotions."