[comp.lang.c] Constraints on UINT_MAX

martin@mwtech.UUCP (Martin Weitzel) (11/14/90)

In article <14379@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes:
>In article <ENAG.90Nov3145415@hild.ifi.uio.no> enag@ifi.uio.no (Erik Naggum) writes:
>-In article <1990Nov2.182217.13958@NCoast.ORG> catfood@NCoast.ORG (Mark W. Schumann) writes:
>-   Spiffy, but it does depend on (exponent & 1) being the same as saying
>-   "exponent is odd."  Most implementations support this, though.
>
>That's good, because the C standard requires this, for positive integers.

What about ((unsigned) exponent & 1)  ?

As I read the ANSI-Standard, in case of a negative exponent,
`(unsigned) exponent' yields the value `UINT_MAX + exponent + 1'.

Now: I've found no explicit guarantee in the standard about the value
of UINT_MAX, other than its minimum is 65535. It would seem logical to
me that the `pure binary representation' also enforces that UINT_MAX
is allways one less than a power of two, which in turn would mean it
is always odd, which would make `(unsigned) exponent & 1' a working
substitute for `exponent % 2' for all values of exponent.

On the other hand, could some hypothetical architecture reserve certain
bit-patterns in an unsigned for special use, and make my above assumption
about the value of UINT_MAX invalid?

Are there other constraints in the standard, which, may be indirectly,
still require constraints on UINT_MAX? Is there anything in the standard
that rules out an implementation with an UINT_MAX of, say, 87944?

For example: The Bit-Shift Operator doesn't mention the possibility that
shifting some bit pattern may result in illegal values. Furthermore it
guarantees for left shift of an unsigned operand the equivalence to
multiplication by powers of two. For multiplication of unsigned the
overflow problem is well defined (result modulo UINT_MAX + 1). With an
UINT_MAX of 87944 not all the above could hold true.

There are two assumptions I'm not sure of:
a) Can the shift operator be applied to *any* arbitrary bit pattern,
   resulting in a new pattern according to all the defined semantics,
b) Can the above proof for a special case (87944) be generalized to 
   all numbers for UINT_MAX, which don't have the propert? 2**N - 1
   (with integral N)?
-- 
Martin Weitzel, email: martin@mwtech.UUCP, voice: 49-(0)6151-6 56 83

gwyn@smoke.brl.mil (Doug Gwyn) (11/16/90)

In article <954@mwtech.UUCP> martin@mwtech.UUCP (Martin Weitzel) writes:
>It would seem logical to me that the `pure binary representation' also
>enforces that UINT_MAX is allways one less than a power of two, ...

I think that could be concluded from the various constraints etc., but
it would be useful to get an official interpretation ruling on this.

>..., which would make `(unsigned) exponent & 1' a working
>substitute for `exponent % 2' for all values of exponent.

True, although that's relying on implementors to get it right, which
on a one's complement architecture requires extra code generation.