Why isn't the 64th battle number printed?



  • 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 &lt;&lt;8;
    x24 = x8 &lt;&lt;16;
    x32 = x8 &lt;&lt;24;
    x40 = x8 &lt;&lt;32;
    x48 = x8 &lt;&lt;40;
    x56 = x8 &lt;&lt;48;
    x64 = x8 &lt;&lt;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's what's sealed:

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

    Why are the values of more than 32 battles not sealed, despite the fact that the variables are the 64th battle?

    EDITED:

    Thank you!
    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 &lt;&lt;8;
    x24 = x8 &lt;&lt;16;
    x32 = x8 &lt;&lt;24;
    x40 = (uint64_t)x8 &lt;&lt;32;
    x48 = (uint64_t)x8 &lt;&lt;40;
    x56 = (uint64_t)x8 &lt;&lt;48;
    x64 = (uint64_t)x8 &lt;&lt;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;
    

    }



  • Because you're using the %x format, which is counted on 32-bit numbers. For the 64-bits, we have to write this:

    unsigned long long foo = 0x123456789ABCDEF0;
    printf ("0x%llX\n", foo);
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2