Re:GCC should warn about 2^16 and 2^32 and 2^64
by fefe23 on Monday June 17, 2019 @05:28AM
(#20201230)
Even if it meant power, it would make sense to warn here, because storing 2^16 in an unsigned short yields 0 :-)
[ Parent
| on HN ]
Re:GCC should warn about 2^16 and 2^32 and 2^64
by garmaine on Monday June 17, 2019 @05:52AM
(#20201358)
Yeah my own first reaction was “Oh, gcc doesn’t warn about overflow assignment? That’s a definite bug.”
I code in C/C++ too, although not exclusively. I facepalmed a few seconds later, but it goes to show the value of having this be a compiler warning.
Re:GCC should warn about 2^16 and 2^32 and 2^64
by cfstras on Monday June 17, 2019 @05:30AM
(#20201240)
these are signed, so you‘re getting -2^16... so many warnings to emit!
/edit: had that mixed up in my head. Interestingly, clang does warn about the overflow:
main.c:6:17: warning: implicit conversion from 'int'
to 'int16_t' (aka 'short') changes value from
65536 to 0 [-Wconstant-conversion]
int16_t s16 = 65536;
Re:GCC should warn about 2^16 and 2^32 and 2^64
by enedil on Monday June 17, 2019 @05:40AM
(#20201282)
Oops, you meant -2^15 I guess.
Re:GCC should warn about 2^16 and 2^32 and 2^64
by NikkiA on Monday June 17, 2019 @06:44AM
(#20201606)
No, they meant -2^16, ie, -(0)
Re:GCC should warn about 2^16 and 2^32 and 2^64
by majewsky on Monday June 17, 2019 @07:20AM
(#20201816)
-2^16 is not -0. The unary minus binds more tightly than the binary xor, so this comes out as
-2^16 = (-2) ^ 16 = 0b111...1110 ^ 0b10000 = 0b111...11011110 = -18
if I'm not mistaken.
Re:GCC should warn about 2^16 and 2^32 and 2^64
by NikkiA on Monday June 17, 2019 @03:11PM
(#20206342)
No, we mean the power operator, 2 to the power of 16 would be '0' in a short int, since the value overflows...
But since philosophically a signed int has '1's in every bit from the MSB to infinity towards the left, it should thus be -0 not 0.