[comp.lang.c] Hexadecimal/octal constants

rjohnson@shell.com (Roy Johnson) (03/20/91)

Is it true on every platform that 0xf == 15?
Is it also true on every platform that for bitwise operations, C
behaves as if it is on a two's complement machine, so that, e.g., the
result of 0xf & 0x2 is 0x2, even if the machine representation of
0xf is 1010 and 0x2 is 0101 [think hypothetical here 8^)]?
--
======= !{sun,psuvax1,bcm,rice,decwrl,cs.utexas.edu}!shell!rjohnson =======
Feel free to correct me, but don't preface your correction with "BZZT!"
Roy Johnson, Shell Development Company

torek@elf.ee.lbl.gov (Chris Torek) (03/21/91)

In article <RJOHNSON.91Mar20091144@olorin.shell.com> rjohnson@shell.com
(Roy Johnson) writes:
>Is it true on every platform that 0xf == 15?

Yes.

>Is it also true on every platform that for bitwise operations, C
>behaves as if it is on a two's complement machine,

No.

>so that, e.g., the result of 0xf & 0x2 is 0x2 ...

Yes.  This latter is guaranteed because both 15 and 2 are positive.

Values of type <unsigned T> (where T is any valid type) *are* `pure
binary'; other integral values `act like' pure binary as long as they
are nonnegative.
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 415 486 5427)
Berkeley, CA		Domain:	torek@ee.lbl.gov

gwyn@smoke.brl.mil (Doug Gwyn) (03/21/91)

In article <RJOHNSON.91Mar20091144@olorin.shell.com> rjohnson@shell.com (Roy Johnson) writes:
>Is it true on every platform that 0xf == 15?

Yes.

>Is it also true on every platform that for bitwise operations, C
>behaves as if it is on a two's complement machine, ...

No.

>... so that, e.g., the
>result of 0xf & 0x2 is 0x2, even if the machine representation of
>0xf is 1010 and 0x2 is 0101 [think hypothetical here 8^)]?

The bitwise operations are carefully specified in the C standard
to have the "right" properties.  However, twos complement has
nothing to do with this.

brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (03/21/91)

In article <RJOHNSON.91Mar20091144@olorin.shell.com> rjohnson@shell.com (Roy Johnson) writes:
> Is it true on every platform that 0xf == 15?

Yes.

> Is it also true on every platform that for bitwise operations, C
> behaves as if it is on a two's complement machine, so that, e.g., the
> result of 0xf & 0x2 is 0x2,

Yes, the result of 0xf & 0x2 is 0x2. No, C does not ``behave as if it is
on a two's complement machine'' for bitwise operations: two's complement
has to do with the representation of negatives, and the bit operators
basically treat everything as unsigned. (Apparently ANSI implies that
any implementation must be one's-complement or two's-complement, but
this is beside the point.)

C does behave as if it is on a binary machine when it is working with
binary digits, even if it is on a base-10 machine or whatever else.

> even if the machine representation of
> 0xf is 1010 and 0x2 is 0101 [think hypothetical here 8^)]?

BEEP! :-) The machine representation is irrelevant. ``If the machine
represents 2 as 0101 and 5 as 1010, does 2 + 2 equal 5?''

---Dan

gwyn@smoke.brl.mil (Doug Gwyn) (03/21/91)

In article <11206@dog.ee.lbl.gov> torek@elf.ee.lbl.gov (Chris Torek) writes:
>In article <RJOHNSON.91Mar20091144@olorin.shell.com> rjohnson@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.