#include <stdio.h> #include <stdint.h> uint32_t replace_field(uint32_t reg, uint32_t val, uint8_t pos, uint8_t len) { // Your code here if(len==0 || (len+pos)>32) return reg; uint32_t mask=((1u<<len)-1)<<pos; reg &= ~mask; reg|=(val<<pos)&mask; return reg; //Input: reg = 0b1111 1111, val = 0b0000, pos = 4, len = 4 //Output: 0b0000 1111 } int main() { uint32_t reg, val; uint8_t pos, len; scanf("%u %u %hhu %hhu", ®, &val, &pos, &len); printf("%u", replace_field(reg, val, pos, len)); return 0; }
Test Cases
Test Results
Input
255 0 4 4
Expected Output
15