Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utcs!mnetor!seismo!caip!clyde!cbatt!cbosgd!ihnp4!ihlpl!saba From: saba@ihlpl.UUCP Newsgroups: net.lang.c Subject: Re: Shift by negative quantity Message-ID: <1093@ihlpl.UUCP> Date: Fri, 11-Jul-86 09:25:54 EDT Article-I.D.: ihlpl.1093 Posted: Fri Jul 11 09:25:54 1986 Date-Received: Sat, 12-Jul-86 02:02:03 EDT References: <894@bu-cs.UUCP> <252@sdchema.sdchem.UUCP> Organization: AT&T Bell Laboratories Lines: 34 > 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