ADC Sample Union View

#include <iostream>
#include <iomanip>
#include <cstdint>
using namespace std;

// Define struct AdcSample here
struct AdcSample {
    uint32_t timestamp;
    union {
        uint16_t raw;
        struct {
            uint8_t lo : 8;
            uint8_t hi : 8;
        };
    };
};

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" << uppercase << hex << setw(2) << setfill('0') << (int)sample.lo
         << " hi=0x" << uppercase << hex << setw(2) << setfill('0') << (int)sample.hi;

    return 0;
}

Solving Approach

 

 

 

 

Upvote
Downvote
Loading...

Input

1000 0x0000

Expected Output

ts=1000 lo=0x00 hi=0x00