gnugo-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [gnugo-devel] sign bit bit shift issue


From: Arend Bayer
Subject: RE: [gnugo-devel] sign bit bit shift issue
Date: Sun, 9 Feb 2003 03:01:57 +0100 (CET)

Hellwig wrote:

> On 08-Feb-03 Arend Bayer wrote:
> >
> > According to the C standard, the patch below fixes a bug. Rotating into
> > the sign bit of a signed integer is undefined.
> >
> > Does anyone happen to know whether this is a problem in real world?
> > (I.e. do there exist platforms where (1 << 31 == 0x8000000) does not
> > hold true?)
>
> I quote from Kernighan & Ritchie, The C Programming Language,
> Second Edition (ANSI C), A7.8 (page 206):
(...)
> So according to this text (which admittedly isn't the standard) the only
> problem seems to be a right-shift, done with a signed integral type which
> has a negative value. This of course relates to the implementation of right
> shifts with or without sign extension, which is left to the implementor.
>
> Arend, did you find this in the standards document? I never heard of
> problems shifting INTO the sign bit.

I only have a draft of C99 here (Committe draft August 3, 1998). It says
(6.5.7 no. 4):

The result of E1 << E2 is E1 left-shifted E2 bit position; vacated bits
are filled with zeros. If E1 has unsigned type...
If E1 has signed type, and nonnegative value, and E1 x 2^E2 is
representable result type, then that is the resulting value; otherwise
the behaviour is undefined.

(And according to no. 3, the result type is the type of E1.)

This seems to say clearly that 1 << 31 is undefined for 32bit ints.

Arend






reply via email to

[Prev in Thread] Current Thread [Next in Thread]