Beginning C question.

Andrew Arensburger arensb at cvl.umd.edu
Wed Jul 25 10:33:18 AEST 1990


In article <25440 at nigel.udel.EDU> gdtltr at freezer.it.udel.edu (Gary Duzan) writes:
>In article <7703 at uudell.dell.com> jrh at mustang.dell.com (James Howard) writes:
>=>main()
>=>{
>=>        short a=0x20df;
>=>        short b=0x3244;
>=>        int   c;
>=>
>=>c = (a<<16) + b;
>=>
>=>I used "short" because they're 16 bits on this machine, and int's are 32.
>=>
>   Doesn't this depend on the implementation of putw and endianness?

	Only if you're reading from a file, or if you originally got
'a' and 'b' by reading individual bytes from memory.
	James's example works correctly because the following assumptions
are true:
	1) The bits in 'a' are arranged in the order in which they
should appear in 'c'.
	2) The bits in 'b' are arranged in the order in which they
should appear in 'c'.
	3) Each bit in 'a' is more significant than any bit in 'b'.
	4) 'b' is 16 bits long.

	Thus, GIVEN that 'a' is the correct most significant half of 'c',
and GIVEN that 'b' is the correct least significant half of 'c', THEN
the statement 'c = (a << 16) + b;' is correct.
	If any of the givens are false, then the result is also false.
And yes, you're right: the givens depend on byte order for longs and
shorts.
	My apologies to anyone who's more confused now than before the
question was raised.

-- 
-------------------------------------------------------------------\\\\^
Andrew Arensburger            | K&R C!   |  "Avoid the (void)"         o\\\\\-
...!uunet!mimsy!cvl!arensb    | ANSI no! |       -- Domino's       __   /
arensb at cvl.umd.edu            |          |          C compiler       \_/



More information about the Comp.lang.c mailing list