// works by casting pointer - gfvalvo
// https://forum.arduino.cc/t/splitting-a-long-long-int/1225989/3?u=dougp
// use pointer to swap upper and lower 32-bit halves
// result printed to serial monitor
unsigned long long _64BitA = 0x1111222233334444;
unsigned long long _64BitB;
void setup() {
Serial.begin(115200);
unsigned long upper32=1, lower32=2, swap32=0;
uint8_t *ptrA = reinterpret_cast<uint8_t *>(&_64BitA);
uint8_t *ptrB = reinterpret_cast<uint8_t *>(&_64BitB);
upper32+=1;
lower32=upper32;
memcpy(&upper32, ptrA + 4, 4);
Serial.print("upper 32 bits \t");
Serial.println(upper32, HEX);
memcpy(&lower32, ptrA, 4);
Serial.print("lower 32 bits \t");
Serial.println(lower32, HEX);
// Serial.println("swapping...\n");
swap32 = upper32; // swap upper/lower
upper32 = lower32;
lower32 = swap32;
memcpy(ptrB, &upper32, 4); // Place swapped values back
memcpy(ptrB + 4, &lower32, 4); // into a 64-bit variable
Serial.print("upper 32 bits \t"); // and print
Serial.println(upper32, HEX);
Serial.print("lower 32 bits \t");
Serial.println(lower32, HEX);
}
void loop() {
}