Beginning C question.
Richard A. O'Keefe
ok at goanna.cs.rmit.oz.au
Thu Jul 26 11:39:44 AEST 1990
In article <4561 at cvl.umd.edu>, arensb at cvl.umd.edu (Andrew Arensburger) writes:
> 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:
> >=> 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 at goanna.cs.rmit.oz.au
I'm afraid you just asked one of the wrong ones. | (quote from Playfair)
More information about the Comp.lang.c
mailing list