(unsigned)-1 (Was: draft ANSI standard: one change that would

karl at haddock.UUCP karl at haddock.UUCP
Tue Dec 16 12:19:36 AEST 1986


In article <518 at brl-sem.ARPA> ron at brl-sem.ARPA (Ron Natalie <ron>) writes:
>Second, nowhere is it stated that (unsigned) -1 will give you a word of
>all ones.  Becareful when making this assumption.  I spend a lot of time
>fixing up the Berkeley network code because of this.

I presume you're thinking of one's complement machines, where the internal
representation of (int)-1 does not have all bits set.  However, as stated in
K&R (Appendix A, Section 6.5), "The value [after converting a signed integer
to an unsigned] is the least unsigned integer congruent to the signed integer
(modulo 2**wordsize)."  Thus, on a 16-bit machine (unsigned)-1 denotes 65535,
which does indeed have all bits set.  Moreover, the same paragraph states that
"in a two's complement representation, this conversion is conceptual and there
is no actual change in the bit pattern"; the inclusion of that first phrase
suggests that the conclusion is not valid in other representations.  (X3J11
uses similar language, I believe.)

I've never used C on non-two's-complement machines -- do they obey this?

Of course, the *correct* way to get a word full of ones is "~(unsigned)0".

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint



More information about the Comp.lang.c mailing list