#include <iostream>
#include <iomanip>
#include <cstdint>
using namespace std;
struct AdcSample {
uint32_t timestamp;
union {
uint16_t raw;
struct {
uint8_t lo;
uint8_t hi;
};
};
};
int main() {
uint32_t ts;
uint16_t raw;
cin >> ts >> std::hex >> raw;
AdcSample sample;
sample.timestamp = ts;
sample.raw = raw;
cout << "ts=" << sample.timestamp
<< " lo=0x" << hex << uppercase << (int)sample.lo
<< " hi=0x" << hex << uppercase << (int)sample.hi;
return 0;
}
Solution Details
👉 In simple words: The union lets you look at the same ADC reading as either a full 16-bit number or split into low/high bytes — and the anonymous union saves you from typing an extra union name.
Significance for Embedded Developers
Input
1000 0x03F2
Expected Output
ts=1000 lo=0xF2 hi=0x3