4 ms·
No. ~0 has type signed int, which on a one's complement machine is a negative zero. Converting this to an unsigned type yields zero. On a sign-magnitude mach
by mansr 15y ago
No. ~0 has type signed int, which on a one's complement machine is a negative zero. Converting this to an unsigned type yields zero. On a sign-magnitude machine, ~0 is -0x7fffffff (assuming 32-bit for simplicity). Conversion to unsigned 32-bit is done by adding 0x100000000, yielding 0x80000001, again not what was desired. If the variable being assigned to has a width different from that of int, things get even more interesting.
- tptacek 15y agoAh. Thanks! Is there a ones-complement or sign-magnitude architecture still in production anywhere? (I'm curious; I have no idea.)
- joel4039 15y agoAs far as I know analog devices still makes some ones-complement Analog to Digital Converters. But I don't know of any modern processor that uses them.
- wglb 15y agoHow about ~0U?
- mansr 15y agoThat will only set as many bits as are in an int, which is wrong if the type of the variable you are setting is wider than int. For a long, you could of course use ~0ul, and so on for other types, but then you'd have to find and update every such assignment if the variable were ever changed to a wider type.