Is this a bug?

Falk Hueffner falk.hueffner@student.uni-tuebingen.de
Thu Feb 6 22:18:00 GMT 2003


Matthew Toseland <toad@amphibian.dyndns.org> writes:

> #include <stdio.h>
> 
> void main() {
>         int x = 1;
>         int y = x << 32;
>         int z = (x << 32);
>         int q = 32;
>         printf("1 << 32 = %x = %x\n", (1L << 32), (1LL << 32));
> 	printf("1 << 32 = %x = %x\n", (1UL << 32), (1ULL << 32));
> 	printf("1 << 32 = %x = %x\n", (x << 32), (z << 32));
>         printf("1 << 32 = %x = %%x\n", %(x << %q), (z %<< q));
> }
> 
> Returns 
> 
> 1 << 32 = 0 = 0
> 1 << 32 = 0 = 0
> 1 << 32 = 1 = 1
> 1 << 32 = 1 = 1
> 
> Is this the defined behaviour?

There is no defined behaviour, so everything is correct.

> (1L << 32) is different from (x << 32) where x is an int defined to
> 1?

Yes, if long is wider than 32 bits. Otherwise, both are undefined.

Maybe we should put this onto the list of non-bugs, it comes up quite
frequently.

-- 
	Falk



More information about the Gcc-bugs mailing list