Path: utzoo!utgpu!attcan!uunet!lll-winken!lll-tis!helios.ee.lbl.gov!pasteur!agate!bionet!apple!rutgers!uwvax!tank!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: question about shift operator Message-ID: <14414@mimsy.UUCP> Date: 8 Nov 88 01:41:51 GMT References: <786@gtx.com> <192@libove.UUCP> Distribution: na Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 37 In article <192@libove.UUCP> root@libove.UUCP (Jay M. Libove) writes: >Looking at the above, I read it to be "shift an n-bit integer n bits left" >... Now, why is there any question as to the result? Because different machines implement shift-left differently. >(One machine test .... Try a better test. #define BITS_PER_CHAR 8 /* needed below */ int fn(); main() { int x = ~0; x <<= fn(sizeof(x)); printf("result of %d << %d is %d\n", ~0, fn(sizeof(x)), x); exit(0); } /* * You may have to put fn() in a separate file if your compiler is * overly clever. */ int fn(sz) int sz; { return (sz * BITS_PER_CHAR); } On the VAX, the result of shifting by the (not known to be constant) value 32 is the same as the result of shifting by zero, because the VAX looks only at the 5 lowest order bits of the shift count. It does this so that right shifts can be done with negative left shifts. (Note that, since this is an arithmetic shift and not a rotate, it still checks the sign bit of the shift count to see whether to propagate the sign bit of the original value in a rightward shift.) -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris