News for hackers. Stuff that matters.
Re:What every compiler writer should know about programmers ... by Kenji on Thursday March 03, 2016 @07:59PM (#11221286)
Yeah, this is undefined behaviour and should not be expected to work, 100% agreed.

Wouldn't it be faster to remove that costly ternary conditional operator by zeroing out the first bit? Well, there's one degenerate case, for minvalue of int, but that is degenerate either way.

Re:What every compiler writer should know about programmers ... by adwn on Friday March 04, 2016 @03:47AM (#11222702)
> Wouldn't it be faster to remove that costly ternary conditional operator by zeroing out the first bit?

No, that wouldn't work for two's-complement numbers [1], only for sign-magnitude numbers (like IEEE floating-point numbers). For example, the value -2 is stored as 11111110 (in an 8-bit signed integer); setting the most significant bit to 0 would result in 01111110, or 126 in decimal.

[1] https://en.wikipedia.org/wiki/Two's_complement

Re:What every compiler writer should know about programmers ... by Kenji on Friday March 04, 2016 @07:54AM (#11223419)
Oh, what an awkward blunder. I know two's complement (that is why I talked about the degenerate case of minvalue). Why did I think this would work?
Re:What every compiler writer should know about programmers ... by adwn on Friday March 04, 2016 @08:11AM (#11223482)
Maybe you were temporarily confused because the MSB is sufficient to test for negativity?