Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> 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



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?


Maybe you were temporarily confused because the MSB is sufficient to test for negativity?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: