Register Overlay byte and bitfield

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

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

int main() {
   int val;
   cin >> val;
   Reg r;
   r.raw = val;
   cout << "en=" << r.bits.en << " mode=" << r.bits.mode;
   return 0;
}
Upvote
Downvote
Loading...

Input

13

Expected Output

en=1 mode=6