No warning promoting longs to long long in gcc

Bug #24762 reported by Christian van den Bosch
6
Affects Status Importance Assigned to Milestone
gcc-3.3 (Ubuntu)
Invalid
Medium
Matthias Klose

Bug Description

// this code can be found at http://cjb.ie/gcc-bug.c or http://cjb.ie/gcc-bug.c.txt

#include <stdio.h>

int main ( int argc, char *argv[] )
{ long long a;
        int b=40;
        a=1<<40; // this generates "warning: left shift count >= width of type",
                  // but no warning about result of 32 bit expression being
implicitly promoted to 64.
                  // this sets a=1.
        printf( "a = %llx\n", a );
        a=1<<b; // this generates no warning at all, and sets
a=1<<(b%32)=0x100 (different to the above case).
        printf( "a = %llx\n", a );
        a=1LL<<b; // this works as intended, seting a=0x10000000000
        printf( "a = %llx\n", a );
}

// I've only tried this with <<, but I'd imagine other operators -
multiplication, for example - are similarly affected.

http://cjb.ie/gcc-bug.c.txt: http://cjb.ie/gcc-bug.c.txt

Revision history for this message
Matthias Klose (doko) wrote :

(In reply to comment #0)
> // this code can be found at http://cjb.ie/gcc-bug.c or
http://cjb.ie/gcc-bug.c.txt
>
> #include <stdio.h>
>
> int main ( int argc, char *argv[] )
> { long long a;
> int b=40;
> a=1<<40; // this generates "warning: left shift count >= width of type",

correct, the right hand side has type int.

> // but no warning about result of 32 bit expression being
> implicitly promoted to 64.

correct, you don't get a warning for promotion to the bigger type.

> // this sets a=1.
> printf( "a = %llx\n", a );
> a=1<<b; // this generates no warning at all, and sets
> a=1<<(b%32)=0x100 (different to the above case).

correct behaviour.

> printf( "a = %llx\n", a );
> a=1LL<<b; // this works as intended, seting a=0x10000000000
> printf( "a = %llx\n", a );
> }
>
> // I've only tried this with <<, but I'd imagine other operators -
> multiplication, for example - are similarly affected.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.