Shifting question

Ray Butterworth rbutterworth at watmath.waterloo.edu
Wed Jul 20 00:53:15 AEST 1988


In article <60290 at sun.uucp>, guy at gorodish.Sun.COM (Guy Harris) writes:
> > action of doing:
> > 	x >>= 16; x>>= 16;
> > better be the same as:
> > 	x = x>>32;
> 
> Perhaps they *should*; however, neither the K&R nor the January 11 ANSI C draft
> specifications for the language require this.  If you think that they should, I
> suggest you lobby the ANSI C committee.

I can see why the Committee should NOT require that (for n>0)
"x >>= n;"  be the same as  "while (n--!=0) x>>=1;".

Imagine a machine with 16 bit words and a logical right shift
instruction that only looks at the lowest 4 bits of the shift size.

"x >>= n;" could simply grab the 4 lower bits of "n" to determine
the size of the shift.  But if the Standard required the requested
behaviour, the compiler would have to generate a lot of extra code
for every shift to change it to a store-zero whenever "n" is
greater than 15. e.g.

LOAD n       Put the shift size into the register.
CMPI #15     Is it bigger than 15?
TLTZ +3
LRS  x           No, shift x right by lower 4 bits of the register
TRA  +2
STZ  x           Yes, so store zero

instead of simply

LOAD n
LRS  x     Shift x right by lower 4 bits of n

No, I don't know of any such machine.
Yes, I know it would be more efficient to change positions of the
shift and store-zero cases.



More information about the Comp.lang.c mailing list