From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1014db,582dff0b3f065a52 X-Google-Attributes: gid1014db,public X-Google-Thread: 109fba,582dff0b3f065a52 X-Google-Attributes: gid109fba,public X-Google-Thread: 103376,bc1361a952ec75ca X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-08-29 10:31:13 PST Path: archiver1.google.com!newsfeed.google.com!postnews1.google.com!not-for-mail From: B_Gaffney@My-Deja.com (B.Gaffney) Newsgroups: comp.lang.ada,comp.lang.c,comp.lang.c++ Subject: Re: Subtle Bugs, kudos Ada (was How Ada ...Red Code ...) Date: 29 Aug 2001 10:31:13 -0700 Organization: http://groups.google.com/ Message-ID: <9f6e2b77.0108290931.35e845b7@posting.google.com> References: <3B8462C8.5596C089@yahoo.com> <87n14nmbf8.fsf@deneb.enyo.de> <3B89A809.46083436@yahoo.com> <871ylxpxu6.fsf@deneb.enyo.de> <3B8AA131.3FCC0E9A@yahoo.com> <87g0ad44ab.fsf@deneb.enyo.de> <3B8B1130.97FFDD66@yahoo.com> <3B8BD171.32C155D2@yahoo.com> <_SRi7.115023$B37.2552767@news1.rdc1.bc.home.com> <3B8BED46.3DEE945B@yahoo.com> NNTP-Posting-Host: 198.26.132.101 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 999106273 6648 127.0.0.1 (29 Aug 2001 17:31:13 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 29 Aug 2001 17:31:13 GMT Xref: archiver1.google.com comp.lang.ada:12565 comp.lang.c:77456 comp.lang.c++:86490 Date: 2001-08-29T17:31:13+00:00 List-Id: Joe Maun wrote in message news:<3B8BED46.3DEE945B@yahoo.com>... > Kaz Kylheku wrote: > > > The entire paragraph from C99 says this: > > > > The result of E1 >> E2 is right shifted E2 bit positions. > > If E1 has an unsigned type, or if E1 has a signed type and > > a nonnegative value, the value of the result is the integral > > part of the quotient of E1 divided by the quantity, 2 raised to > > the power of E2. If E1 has a signed type and a negative value, > > the resulting value is implementation-defined. > > > > So you see, when the result is not implementation-defined, it is actually > > defined *arithmetically* as the quotient of an integer division by a power > > of two. > > The *value* is defined arithmetically, while the bit positions are > defined in terms of bit-shifts. Both are properties of the shift > operator. When one is implementation defined, the other may still be > well defined. What do you think the first sentence is there for? But on a sign-magnitude machine it may make sense to exclude the sign bit from this shift. Is there anything in the standard which specifies the bit layout or what a "right shift" is? Does it specify that it must include the sign bit? >From a practical perspective, if a compiler were implemented on such a machine, it could take advantage of this "implementation-defined" permission to allow it to optimize A/2 as A>>1 (i.e. if A = 42, A/2 = A>>1 = 21 and if B = -42, B/2 = B>>1 = -21), which it couldn't do for a signed type otherwise (assuming a 16-bit sign-magnitude type, B>>1 = 32747 <> B/2). There are two "expectation" people have of a right shift (whatever the standard actually says): it is equivalent to a division by a power of two and the sign bit is shifted. On a sign-magnitude machine one of the two will always fail, and some code with it. It's up the the implementation which way is 'best'. (Good reason to stick with 2's complement.) BTW, what if a machine were implemented with the LSB on the left and the MSB on the right? Would A>>1 actually require a _left_ shift (per the A/2 requirement above)? --Brian