c++ - Why does shifting more than the allowed bits still work? -
i have int8_t
, wanted see happen if shift left further 8 bits. did:
int8_t x = 1; std::cout << (x << 10);
for reason returns 1024 if type contained enough bits represent number. thought when shift more given bits 0 in bits (or signed overflow/underflow leads undefined behavior). also, ran code give me maximum number of int8_t
:
std::numeric_limits<int8_t>::max(); // 127
the max number of type 127 shifting left can make go higher unsigned type! how possible?
the arguments <<
being implicitly widened int
, , result of x << 10
int
.
Comments
Post a Comment