Shift by negative quantity

saba at ihlpl.UUCP saba at ihlpl.UUCP
Fri Jul 11 23:25:54 AEST 1986


> In article <894 at bu-cs.UUCP> bzs at bu-cs.UUCP (Barry Shein) writes:
> >Question: Is shifting by a negative quantity defined (that is,
> >probably means the other shift.) I could find no mention in K&R.
> >...
> 
> Looking up "shift operators" in the index of K&R, I am referred to p. 189:
> 
> 	"The result [of a shift operator] is undefined if the right operand
> 	is negative, or greater than or equal to the length of the object
> 	in bits."
> 
> I'm sure its done this way so that the operation is closer to every machine's
> instruction set and isn't a higher-level operation with additional checks
> or conversions.
> 
> >P.S. Is this one of those questions that gets asked every 2 months?
> 
> I haven't seen it before.
> 
> --Tom Stockfisch, UCSD Chemistry

I've seen some older compilers (PDP-11 things) actually change the
shift operator to the oppsite direction when a negative value is
used. If the "shift by" value is a constant, its easy.

For example, i = x << -3  is the same as i = x >> 3

The library routines would handle negatives if the shift by wasn't
a constant, i.e. i = x << y, where y is negative.

The lastest SYSVR2 over here barfs at shift with negatives.

Bruce Sabalaskey
ihnp4!ihlpl!saba



More information about the Comp.lang.c mailing list