Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!purdue!haven!cvl!arensb From: arensb@cvl.umd.edu (Andrew Arensburger) Newsgroups: comp.lang.c Subject: Re: Beginning C question. Message-ID: <4561@cvl.umd.edu> Date: 25 Jul 90 00:33:18 GMT References: <10997@chaph.usc.edu> <7703@uudell.dell.com> <25440@nigel.udel.EDU> Reply-To: arensb@cvl.umd.edu (Andrew Arensburger) Organization: Loyola College in Baltimore Lines: 39 In article <25440@nigel.udel.EDU> gdtltr@freezer.it.udel.edu (Gary Duzan) writes: >In article <7703@uudell.dell.com> jrh@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@cvl.umd.edu | | C compiler \_/