//
// Created by James Gallegos on 9/23/25.
//
/*
In embedded systems, registers are often configured by setting specific bits.
To make the code cleaner and reusable, firmware developers use macros to set
fields in a register.
You are given a 16-bit control register layout:
Field Bits Position (LSB-first)
ENABLE 1 Bit 0
MODE 2 Bits 1–2
SPEED 3 Bits 3–5
RESERVED 2 Bits 6–7 (must be 0)
Your task is to:
Write macros to:
Set the ENABLE bit
Set the MODE field
Set the SPEED field
Read ENABLE, MODE, SPEED from input
Use the macros to pack a final 16-bit register value
RESERVED bits (6–7) must be left 0
*/
// #define NAME(param1, param2, ..., paramN) expression_using_them
#include <stdio.h>
#include <stdint.h>
#define ENABLE_POS 0
#define MODE_POS 1
#define SPEED_POS 3
#define ENABLE_MASK (0x1u << ENABLE_POS)
#define MODE_MASK (0x3u << MODE_POS)
#define SPEED_MASK (0x7u << SPEED_POS)
// Packs are basically formulas condensed into a single variable
#define PACK_ENABLE(en) ( (uint16_t)(((en) & 0x1u) << ENABLE_POS) )
#define PACK_MODE(md) ( (uint16_t)(((md) & 0x3u) << MODE_POS) )
#define PACK_SPEED(sp) ( (uint16_t)(((sp) & 0x7u) << SPEED_POS) )
uint16_t build_register(uint8_t enable, uint8_t mode, uint8_t speed) {
uint16_t reg = 0; // start with all bits 0 (incl. RESERVED)
reg |= PACK_ENABLE(enable); // bit 0
reg |= PACK_MODE(mode); // bits 1–2
reg |= PACK_SPEED(speed); // bits 3–5
// Bits 6–7 are RESERVED and left 0 by construction
return reg;
}
int main() {
uint8_t enable, mode, speed;
scanf("%hhu %hhu %hhu", &enable, &mode, &speed);
uint16_t reg = build_register(enable, mode, speed);
printf("%u", reg);
return 0;
}
Input
1 2 4
Expected Output
37