Register Overlay byte and bitfield

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

// Define union Reg with:
// - uint8_t raw
// - struct bits { en:1, mode:3, reserved:4 }

struct Reg{
    union{
        uint8_t raw;
        struct {
            uint8_t en : 1;
            uint8_t mode : 3;
            uint8_t reserved : 4;
        }bits;
    };
};

int main() {
    unsigned int input;
    cin >> input;

    uint8_t val = static_cast<uint8_t>(input);

    Reg r;
    r.raw = val;

    // Print numeric values (NOT characters)
    cout << "en=" << static_cast<int>(r.bits.en)
         << " mode=" << static_cast<int>(r.bits.mode);

    return 0;
}

Solving Approach

 

 

 

 

Upvote
Downvote
Loading...

Input

13

Expected Output

en=1 mode=6