Hexadecimal/octal constants

Doug Gwyn gwyn at smoke.brl.mil
Thu Mar 21 10:47:08 AEST 1991


In article <11206 at dog.ee.lbl.gov> torek at elf.ee.lbl.gov (Chris Torek) writes:
>In article <RJOHNSON.91Mar20091144 at olorin.shell.com> rjohnson at shell.com (Roy Johnson) writes:
>>Is it true on every platform that 0xf == 15?

You know, Chris, it occurs to me that perhaps Roy was thinking that "0xf"
was somehow expressing a REPRESENTATION rather than a VALUE.  Even though
most code that uses such constants in bitwise operations might make that
appear plausible, in actuality the decimal, octal, and hexadecimal
constants are merely alternate ways of expressing the same thing, namely
the value.  (There is some effect on the type too, but that's not
relevant to this discussion.)  A standard-conforming C implementation
must ensure that the bitwise operators produce the same results as the
"obvious" implementation on a simple architecture would, but it can use
any manner of gyrations in order to accomplish the right effect.  In
particular, it doesn't have to "really" represent integers in the
"obvious" way, so long as it always acts as though it did when required.

In practice, nearly all C implementations will be on binary-based
architectures, and will use one of the following integral representations,
depending on what the architecture provides the best support for:
	two's complement
	one's complement
	sign, magnitude
The C standard supports efficient use of any of these three.  For other
representation schemes, e.g. decimal-based, at least the bitwise operators
(including shift operators) will have to be implemented with additional
code to in effect convert the operands to a "binary numeration system"
basis before performing the operations.



More information about the Comp.lang.c mailing list