#include <stdio.h> #include <stdint.h> uint8_t set_range(uint8_t reg, uint8_t start, uint8_t end) { // Assumes 0 <= start <= end <= 7 uint8_t width = (uint8_t)(end - start + 1); uint16_t mask16; if (width >= 8) { // Only possible when start=0 and end=7 for an 8-bit register mask16 = 0xFFu; } else { // Build the mask in a wider type to avoid undefined shifts mask16 = ((1u << width) - 1u) << start; } return reg | (uint8_t)mask16; } int main(void) { uint8_t reg, start, end; if (scanf("%hhu %hhu %hhu", ®, &start, &end) != 3) { return 1; } printf("%u", set_range(reg, start, end)); return 0; }
Test Cases
Test Results
Input
0 1 3
Expected Output
14