This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Is this a bug?


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]