Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!seismo!rutgers!mit-eddie!genrad!decvax!ima!haddock!karl
From: karl@haddock.UUCP (Karl Heuer)
Newsgroups: comp.lang.c
Subject: Re: (unsigned)-1
Message-ID: <288@haddock.UUCP>
Date: Wed, 24-Dec-86 15:54:30 EST
Article-I.D.: haddock.288
Posted: Wed Dec 24 15:54:30 1986
Date-Received: Wed, 24-Dec-86 21:38:04 EST
References: <1382@hoptoad.uucp> <690001@hplsla.HP.COM>
Reply-To: karl@haddock.ISC.COM.UUCP (Karl Heuer)
Organization: Interactive Systems, Boston
Lines: 19
Summary: "~0" can be dangerous too

In article <690001@hplsla.HP.COM> bam@hplsla.HP.COM writes:
>Using (unsigned) -1 is asking for trouble.  A better approach is
>    int allones = ~0;

This is probably okay.

>    This works for any size integer.

Well, not quite.  If the type of "~0" is "int" (which it is on this compiler;
no official reference at hand), then "unsigned u = ~0" is equivalent to
"unsigned u = (unsigned)~0", and if "~0" is negative zero, then casting it
from int to unsigned should yield "the value congruent to it mod 2**n", namely
zero! (I presume +0 and -0 are arithmetically equal; never did understand the
logic of ones-complement.)  And on the pdp-11, "long x = ~0" works only
because the 16-bit result of "~0" gets sign-extended.

I'd say the safest notation is "~(target-type)0".

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