#include <stdio.h> #include <stdint.h> uint32_t extract_field(uint32_t reg, uint8_t pos, uint8_t length){ uint32_t x = 0; uint8_t end = pos + length - 1; uint8_t count = 0; for(uint8_t i=pos; i<= end; i++){ x = x | ( ((reg>>i) & 0x1) << count) ; count++; } return x; } int main(){ uint32_t reg; uint8_t pos, length; scanf("%u %hhu %hhu", ®, &pos, &length); printf("%u",extract_field(reg, pos, length)); return 0; }
Test Cases
Test Results
Input
3060793344 28 4
Expected Output
11