I just want to combine my uint8_t array with uint64_t. In fact, I solved my problem, but I need to understand the reason. Here is my code;
uint8_t byte_array[5]; byte_array[0] = 0x41; byte_array[1] = 0x42; byte_array[2] = 0x43; byte_array[3] = 0x44; byte_array[4] = 0x45; cout << "index 0: " << byte_array[0] << " index 1: " << byte_array[1] << " index 2: " << byte_array[2] << " index 3: " << byte_array[3] << " index 4: " << byte_array[4] << endl; uint64_t reverse_of_value = (byte_array[0] & 0xff) | ((byte_array[1] & 0xff) << 8) | ((byte_array[2] & 0xff) << 16) | ((byte_array[3] & 0xff) << 24) | ((byte_array[4] & 0xff) << 32); cout << reverse_of_value << endl; reverse_of_value = (uint64_t)(byte_array[0] & 0xff) | ((uint64_t)(byte_array[1] & 0xff) << 8) | ((uint64_t)(byte_array[2] & 0xff) << 16) | ((uint64_t)(byte_array[3] & 0xff) << 24) | ((uint64_t)(byte_array[4] & 0xff) << 32); cout << reverse_of_value << endl;
The first output will be "44434245" and the second will be "4544434241", which I want.
Thus, as we see, when I use casting of each byte for the uint64_t code, however, if I do not use casting, this gives me an irrelevant result. Can anyone explain the reason?
ergin source share