draft ANSI standard: one change that would *really* help Europe
bam at hplsla.HP.COM
bam at hplsla.HP.COM
Wed Dec 17 09:20:12 AEST 1986
In article <518 at brl-sem.ARPA> ron at brl-sem.ARPA (Ron Natalie <ron>) writes:
>> ... 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.
>
> Actually, I think (unsigned) -1 does have to give you a bit pattern of
>all 1's. I can not find an explicit reason, but I can deduce this from the
>following:
>
>--------
>Kenneth R. Ballou ARPA: ballou at brahms
>Department of Mathematics UUCP: ...!ucbvax!brahms!ballou
>University of California
>Berkeley, California 94720
>----------
Try evaluating (unsigned) -1 on a one's complement machine! A word
containing all ones in such a machine (e.g. CDC Cyber machines) is
taken to be "negative zero", which is really an illegal representation.
Using (unsigned) -1 is asking for trouble. A better approach is
to use the bitwise complement operator '~':
int allones = ~0;
This works for any size integer.
Ben Mejia
Hewlett-Packard Lake Stevens Instrument Division
Everett WA
More information about the Comp.lang.c
mailing list