(unsigned)-1
Karl Heuer
karl at haddock.UUCP
Thu Dec 25 06:54:30 AEST 1986
In article <690001 at hplsla.HP.COM> bam at 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 at haddock.isc.com), The Walking Lint
More information about the Comp.lang.c
mailing list