I am interacting with hardware that gives me various kinds of data packed away inside arrays of uint32_t
. Unfortunately, I've forgotten how to interpret one of those as a uint64_t
.
#include <iostream>
#include <cstdint>
int main()
{
const uint64_t x = 123456789123456789ULL;
// Pack into uint32_t[2]
uint32_t ar[2] = {};
ar[0] = x >> 32;
ar[1] = x & 0xFFFF;
// Unpack into uint64_t
uint64_t y;
y = ar[0];
y = (y << 32) + ar[1];
// The output should be the same!
std::cout << x << ' ' << y << std::endl;
}
// Output: 123456789123456789 123456786224144149
What painfully obvious mistake am I making?