My code is:

#include <stdio.h> #include <stdint.h> int main(void) { uint8_t x8 = 0xFF; uint16_t x16; uint32_t x32, x24; uint64_t x64, x56, x48, x40; x16 = x8 <<8; x24 = x8 <<16; x32 = x8 <<24; x40 = x8 <<32; x48 = x8 <<40; x56 = x8 <<48; x64 = x8 <<56; printf("\n"); printf("x8 = 0x%016x\n", x8); printf("x16 = 0x%016x\n", x16); printf("x24 = 0x%016x\n", x24); printf("x32 = 0x%016x\n", x32); printf("x40 = 0x%016x\n", x40); printf("x48 = 0x%016x\n", x48); printf("x56 = 0x%016x\n", x56); printf("x64 = 0x%016x\n", x64); return 0; } 

Here is what is printed:

 x8 = 0x00000000000000ff x16 = 0x000000000000ff00 x24 = 0x0000000000ff0000 x32 = 0x00000000ff000000 x40 = 0x0000000000000000 x48 = 0x0000000000000000 x56 = 0x0000000000000000 x64 = 0x0000000000000000 

Why are values ​​larger than 32 bits not printed out, despite the fact that variables are 64-bit?

EDITED:

Thank you @Mark Shevchenko and @ Vladimir Martyanov !!!
The working code is:

 #include <stdio.h> #include <stdint.h> int main(void) { uint8_t x8 = 0xFF; uint16_t x16; uint32_t x32, x24; uint64_t x64, x56, x48, x40; x16 = x8 <<8; x24 = x8 <<16; x32 = x8 <<24; x40 = (uint64_t)x8 <<32; x48 = (uint64_t)x8 <<40; x56 = (uint64_t)x8 <<48; x64 = (uint64_t)x8 <<56; printf("\n"); printf("x8 = 0x%016llX\n", x8); printf("x16 = 0x%016llX\n", x16); printf("x24 = 0x%016llX\n", x24); printf("x32 = 0x%016llX\n", x32); printf("x40 = 0x%016llX\n", x40); printf("x48 = 0x%016llX\n", x48); printf("x56 = 0x%016llX\n", x56); printf("x64 = 0x%016llX\n", x64); return 0; } 

    2 answers 2

    As far as I can see, the problem is that you must cast x8 to uint64_t before shifting the value.

     x40 = x8 << 32; 

    In this case, on 32-bit machines, operations on x8 will be performed in the same way as on a 32-bit number (anything less than an int cast to an int ).

    Therefore, when shifting to 32 bits, an overflow will occur. To avoid this, you need to write like this:

     x40 = (uint64_t)x8 << 32; 
    • Yes, you are right :) Just before your answer, it dawned me :) - Michael Vaysman

    Because you use the format% x, which is designed for 32-bit numbers. For 64-bit you need to write something like this:

     unsigned long long foo = 0x123456789ABCDEF0; printf ("0x%llX\n", foo); 
    • So it does not work either. Displays the following: x8 = 0xBFA4E8DC000000FF, x16 = 0xBFA4E8DC0000FF00, x24 = 0xBFA4E8DC00FF0000, x32 = 0xBFA4E8DCFF000000, x40 = 0x0, x48 = 0x0, x56 = 0x0, x64 = 0x0x0 = 0x0, x48 = 0x0, x56 = 0x0, x56 = 0xBFA4E8DCFF00000000
    • You copy my code and check that it works as it should. Here he works for me, specially checked. - Vladimir Martyanov
    • But it can not be that the problem is due to the fact that I use uint64_t, and you are unsigned long long? Although they should be the same ... - Michael Vaysman
    • Pro 32/64 is not quite right, because the size in bytes of integer types in the standard is not regulated. Therefore, %x is for unsigned int , and the prefix ll will say that it is already unsigned long long int . - αλεχολυτ
    • @alexolut I have not met with unsigned long more than 32 bits, to be honest :-) - Vladimir Martyanov