Beginner here, learning c++. I tried to convert a negative int16_t (-32768) to uint32_t and I'm not exactly sure why I get 4294934528. The binary representation of -32678 as a short is a one followed by 15 zeroes so shouldn't casting just add 16 zeroes to the left of original binary representation and give me a positive int?
How does c++ handle type casting a 16 bit integer (especially a negative int) to an unsigned 32 bit?
Asked
Active
Viewed 190 times
0
-
Casting generally performs a value conversion. It doesn't just reinterpret bits. – melpomene Sep 28 '17 at 03:51
-
Oh so how will the value be converted in this case? – northernregime Sep 28 '17 at 03:53
-
If the linked duplicate doesn't explain it fully, I think the `int16_t` is sign extended to a full `int` before the conversion is done. – Mark Ransom Sep 28 '17 at 04:29
-
More fundamentally, **what do you want the result to be**? That determines what the appropriate conversion code should do. – Pete Becker Sep 28 '17 at 13:26