[net.lang.c] Shift by negative quantity

bzs@bu-cs.UUCP (Barry Shein) (07/05/86)

Question: Is shifting by a negative quantity defined (that is,
probably means the other shift.) I could find no mention in K&R.

it came up with an expression like:

	foo = goo >> 10-PGSHIFT;

which was a constant shift value. it didn't work on a SUN3, what's the
poop? (seems hard for most code generators if it's variable, well,
would have to test and branch if the underlying machine's shift
instruction won't take negative values correctly so I wouldn't be
surprised if it's undefined and I could understand not wanting a
separate rule for a constant even though it would be easy to turn it
around.)

Therefore, I guess it's just not defined and you have to generate
your own test, but I was wondering what was official.

Of course, it accidently works right on a VAX and NS32k, great,
more bugs to worry about...(a test example forcing negative,
I know the above is positive on a VAX, that's the point ultimately.)

	-Barry Shein, Boston University

P.S. Is this one of those questions that gets asked every 2 months?
If so, I'm glad, maybe I'll get a good answer after all that practice :-)

ark@alice.UucP (Andrew Koenig) (07/05/86)

> Question: Is shifting by a negative quantity defined (that is,
> probably means the other shift.) I could find no mention in K&R.

You apparently didn't look on page 189, where it says:

	"The result is undefined if the right operand is negative,
	 or greater than or equal to the length of the object in bits."

chris@umcp-cs.UUCP (Chris Torek) (07/05/86)

In article <894@bu-cs.UUCP> bzs@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.

It is explicitly undefined.  K&R, Appendix A, \S 7.5, Shift Operators
(p. 189):

  The result is undefined if the right operand is negative, or greater
  than or equal to the length of the object in bits.

>P.S. Is this one of those questions that gets asked every 2 months?

No.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@mimsy.umd.edu

tps@sdchem.UUCP (Tom Stockfisch) (07/08/86)

In article <894@bu-cs.UUCP> bzs@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

saba@ihlpl.UUCP (07/11/86)

> In article <894@bu-cs.UUCP> bzs@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