Home > Mobile >  Why GCC long long int can't do 1<<48 but gets an overflow warning
Why GCC long long int can't do 1<<48 but gets an overflow warning

Time:11-27

{
    unsigned long long two16, two48 ;
    two16 = 65536;
    two48 = two16 * two16 * two16 ; 
    printf("2^48=%llX \n",two48 );
    two48 = 1<<48 ;
    printf("Shifted 1<<48=%llX \n",two48);
    return 0 ;
}

When compiled on a 64 bit machine, with a word size of 8, the above gives a warning the two=1<<48 will overflow left shift count >= width of type.

The output of the program is:

    2^48=1000000000000  
    Shifted 1<<48=0

What is going on? Why can I not shift a 64bit quantity 48 bits?

CodePudding user response:

why can I not shift a 64bit quantity 48 bits?

You are not shifting a 64-bit integer by 48. You are shifting a 32-bit integer (1) by 48, and assigning the (overflown) result to a 64-bit integer. Try this instead:

two48 = 1ULL << 48;

or this:

two48 = 1;
two48 = two48 << 48;
  • Related