This question was first inspired by the (unexpected) results of this code:
uint16_t t16 = 0;
uint8_t t8 = 0x80;
uint8_t t8_res;
t16 = (t8 << 1);
t8_res = (t8 << 1);
printf("t16: %x\n", t16);
printf(" t8: %x\n", t8_res);
But it turns out this makes sense:
6.5.7 Bitwise shift operators
Limitations
2 Each operand must be an integer type
Thus, the initially confused string is equivalent to:
t16 = (uint16_t) (((int) t8) << 1);
A bit unintuitive IMHO, but at least clearly defined.
Ok, great, but then we do:
{
uint64_t t64 = 1;
t64 <<= 31;
printf("t64: %lx\n", t64);
t64 <<= 31;
printf("t64: %lx\n", t64);
}
// edit: after the same argument as above, the following should be equivalent:
t64 = (uint64_t) (((int) t64) << 31);
// hence my confusion / expectation [end_edit]
, , () . / " "? - , ( ?), , :
uint32_t << uint64_t
, int; ?
//edit:
, :
uint32_t t32 = 1;
uint64_t t64_one = 1;
uint64_t t64_res;
t64_res = t32 << t64_one;
//end edit
, , int, integer type, uint64_t .
// :
, . , uint8_t , int ? , int 1, :
{
uint16_t t16 = 0;
uint8_t t8 = 0x80;
uint8_t t8_one = 1;
uint8_t t8_res;
t16 = (t8 << t8_one);
t8_res = (t8 << t8_one);
printf("t16: %x\n", t16);
printf(" t8: %x\n", t8_res);
}
t16: 100
t8: 0
(t8 < t8_one) , uint8_t ?
-
, ISO/IEC 9899: TC9, WG14/N1124 6 2005 . , - , .