#include <stdio.h> #include <stdint.h> // Define macros here #define SET_ENABLE(val) ((uint16_t)(val & 0x01) << 0) #define SET_MODE(val) ((uint16_t)(val & 0x03) << 1) #define SET_SPEED(val) ((uint16_t)(val & 0x07) << 3) uint16_t build_register(uint8_t enable, uint8_t mode, uint8_t speed) { uint16_t reg = 0; // Combine the fields using the Bitwise OR operator reg |= SET_ENABLE(enable); reg |= SET_MODE(mode); reg |= SET_SPEED(speed); return reg; } int main() { uint8_t enable, mode, speed; if (scanf("%hhu %hhu %hhu", &enable, &mode, &speed) == 3) { uint16_t reg = build_register(enable, mode, speed); printf("%u\n", reg); } return 0; }
Test Cases
Test Results
Input
1 2 4
Expected Output
37