right/left shifts with (width of type)
Nathan Sidwell
nathan@cs.bris.ac.uk
Thu Jul 2 07:11:00 GMT 1998
Stefan Meixner wrote:
>
> Hi,
>
> right now I will not call it a bug, but gcc/egcs
> practise with right/left shifts with counts equal
> to the width of the type is at least not protable
> (at least on a i486-pc-linux-gnulibc1 where I tested it).
>
> I would expect the following expression to hold
> for both << and >> operators
>
> unsigned long u = ~0;
>
> if ( (u << (sizeof(unsigned long)*CHAR_BIT)) == 0 )
> printf("GOOD");
> else
> printf("BAD");
>
> At least this is the behavior I would expect and I get from
> the other compilers I tested.
>
> I know that this maybe implementation defined. But shifting
> any unsigned long by count=width, the result should be zero.
>
> Am I wrong?
You are wrong. In both C and C++ it is undefined if the shift amount is
negative or greater or equal to the length in bits of the promoted left
operand. That's *undefined*, not implementation defined, anything can
happen.
Your assertion that x << bits_in_x == 0 is not true. Relying on it is
inherently non-portable -- it's not even guaranteed to be consistent
within a single program.
nathan
--
Dr Nathan Sidwell :: Computer Science Department :: Bristol University
You can up the bandwidth, but you can't up the speed of light
nathan@acm.org http://www.cs.bris.ac.uk/~nathan/ nathan@cs.bris.ac.uk
More information about the Gcc-bugs
mailing list