160. Register Overlay byte and bitfield

In embedded systems, hardware registers are often accessed both as a full byte and as individual bit fields.
Your task is to define a union that overlays an 8-bit register with named bitfields in a deterministic and embedded-safe manner.

You must define a union Reg that contains:

  • A raw 8-bit value raw of type uint8_t
  • A nested struct bits with the following fields:
    • en : 1-bit enable flag (bit 0, least significant bit)
    • mode : 3-bit mode field (bits 1–3)
    • reserved : 4 bits (bits 4–7, unused)

Assumptions (Explicit and Required):

  • Bit 0 is the least significant bit (LSB)
  • The target platform uses an 8-bit byte
  • The compiler allocates bitfields from LSB to MSB within a byte
  • This pattern reflects common embedded firmware practice for memory-mapped registers

The program will:

  1. Read an integer value in the range 0–255
  2. Assign it to the register
  3. Print the decoded en and mode fields as numeric values

Input

A single integer value N

  • 0 ≤ N ≤ 255

Output

Two numeric values printed on a single line:

en=<value> mode=<value>

Example:

Input

13

Output

en=1 mode=6 

Explanation

13 = 0b00001101

bit 0        → en = 1
bits 1–3    → mode = 110b = 6
bits 4–7    → reserved

 

 

 

 

Loading...

Input

13

Expected Output

en=1 mode=6