Result of shift when operand == type size

Michael Meissner meissner@cygnus.com
Tue Apr 25 09:45:00 GMT 2000


On Tue, Apr 25, 2000 at 10:30:58AM +0200, Kai Harrekilde-Petersen wrote:
> According to K&R (2ed), pg 206, the result of a shift,
> is undefined "if right operand is negative, or greater than or
> equal to the number of bits in the left expression's type"
> (A7.8 Shift Operators).
> 
> GCC follows this a bit too faithfully, IMHO. Specifically,
> ~(~0ULL<<64) != ~0ULL. This complicates writing bitmask macros.
> 
> Is it possible to change the current behaviour?
> (preferably, I'd like to see the C standard changed, but that
> would be asking for the moon, I guess).

No it is not possible.  Off the top of my head here is what some of the
machines that GCC supports does with such a shift (where N is 32 or 64
depending on the wordsize):

   1)	shift N bits (ie, for 1<<N the result is 0);
   2)	shift 0 bits (ie, for 1<<N the result is 1);
   3)	extract the bottom bit (ie, for 3<<N the result is 1).

If N is negative, then some machines will do the opposite shift, some will mask
the number of bits (ie #2).

-- 
Michael Meissner, Cygnus Solutions, a Red Hat company.
PMB 198, 174 Littleton Road #3, Westford, Massachusetts 01886, USA
Work:	  meissner@redhat.com		phone: +1 978-486-9304
Non-work: meissner@spectacle-pond.org	fax:   +1 978-692-4482


More information about the Gcc-bugs mailing list