(unsigned) -1 on one's complement machines

Joe Buck jbuck at epimass.UUCP
Sat Dec 20 03:42:27 AEST 1986


In article <690001 at hplsla.HP.COM> bam at hplsla.HP.COM writes:
>    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.

Ben, you're wrong, though it's a common mistake.  It is true that if
I say

	int i = -1;

on a one's complement machine that the pattern is not all ones.  But

	(unsigned) i

on a one's complement machine does NOT mean "take the bit pattern in
i as an unsigned value".  K&R explicitly say that a conversion takes
place:  i is taken modulo 2^wordsize.  Thus (unsigned)-1 is 65535 on
ANY 16-bit machine, one's complement, two's complement,
sign-magnitude or what have you.  Otherwise the compiler is broken;
apparently the original author had a broken compiler.  Someone
probably ported pcc or something to the one's complement machine
without noting that (unsigned) is no longer a no-op.

It is true that

>    int allones = ~0;

works, and if this bug is common in compilers for one's complement
machines it may be safer to use.
-- 
- Joe Buck 	{hplabs,ihnp4,sun}!oliveb!epimass!jbuck		HASA (A,S)
  Entropic Processing, Inc., Cupertino, California



More information about the Comp.lang.c mailing list