I have a an issue where I can’t serialize a 64bit integer (32bit works)
Code is as follows:
uint64_t t = (uint64_t) 0;
uint8_t buffer[8];
buffer[0] = 0x12;
buffer[1] = 0x34;
buffer[2] = 0x56;
buffer[3] = 0x78;
buffer[4] = 0x9A;
buffer[5] = 0xBC;
buffer[6] = 0xDE;
buffer[7] = 0xF0;
printf("uint64_t width: %lu\n",sizeof(t));
t |= (uint64_t) ( (buffer[7] << (7*8)) & 0xFF00000000000000 );
t |= (uint64_t) ( (buffer[6] << (6*8)) & 0x00FF000000000000 );
t |= (uint64_t) ( (buffer[5] << (5*8)) & 0x0000FF0000000000 );
t |= (uint64_t) ( (buffer[4] << (4*8)) & 0x000000FF00000000 );
t |= (uint64_t) ( (buffer[3] << (3*8)) & 0x00000000FF000000 );
t |= (uint64_t) ( (buffer[2] << (2*8)) & 0x0000000000FF0000 );
t |= (uint64_t) ( (buffer[1] << (1*8)) & 0x000000000000FF00 );
t |= (uint64_t) ( (buffer[0]) & 0x00000000000000FF );
printf("uint64 value: 0x%llu\n",t);
however the compiler is warning me I have bit-shifted too far for the upper 32 bits. The sizeof operator is telling me its 64bit width though?
output is:
uint64_t width: 8
uint64 value: 0x78563412
Whats going on here?
You need to cast before you shift, e.g.
Actually you don’t even need the mask so this could just be simplified to: