Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!seismo!epiwrl!epimass!jbuck
From: jbuck@epimass.UUCP (Joe Buck)
Newsgroups: comp.lang.c
Subject: (unsigned) -1 on one's complement machines
Message-ID: <747@epimass.UUCP>
Date: Fri, 19-Dec-86 12:42:27 EST
Article-I.D.: epimass.747
Posted: Fri Dec 19 12:42:27 1986
Date-Received: Sat, 20-Dec-86 01:44:39 EST
References: <1382@hoptoad.uucp> <690001@hplsla.HP.COM>
Reply-To: jbuck@epimass.UUCP (Joe Buck)
Organization: Entropic Processing, Inc., Cupertino, CA
Lines: 32
Summary: yes, this IS portable and always gives all ones

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