Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!sdd.hp.com!elroy.jpl.nasa.gov!ames!uhccux!virtue!comp.vuw.ac.nz!munnari.oz.au!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: Beginning C question. Message-ID: <3472@goanna.cs.rmit.oz.au> Date: 26 Jul 90 01:39:44 GMT References: <10997@chaph.usc.edu> <7703@uudell.dell.com> <25440@nigel.udel.EDU> <4561@cvl.umd.edu> Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 27 In article <4561@cvl.umd.edu>, arensb@cvl.umd.edu (Andrew Arensburger) writes: > 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: > >=> short a=0x20df; > >=> short b=0x3244; > >=> int c; > >=> > >=> c = (a<<16) + b; > >=> > >=>I used "short" because they're 16 bits on this machine, and ints are 32. > James's example works correctly because the following assumptions > are true: Bad news, friends, the example DOESN'T work correctly, even if the half-word sex of the machine is right. Suppose the high bit of b is 1. That's the _sign_ bit, remember. The effect in that case is as if 1 were subtracted from a. (I tried it, and that _does_ happen.) Instead, use c = (a << 16) | (b & 0xFFFF); or make a and b "unsigned short" and do c = (int)((a << 16) + b); -- Science is all about asking the right questions. | ok@goanna.cs.rmit.oz.au I'm afraid you just asked one of the wrong ones. | (quote from Playfair)