Extract Bit Field from 16-bit Register

Code

#include <stdio.h>
#include <stdint.h>

uint8_t extract_field(uint16_t reg) {
    // Your logic here
    
    reg= (reg >> 4) & 0x1F;
    return (uint8_t) reg;
}

int main() {
    uint16_t reg;
    scanf("%hx", &reg);
    printf("%u", extract_field(reg));
    return 0;
}

Solving Approach

Right shift the register to get the bits in focus then bitwise AND with 0x1F

preserve only the bits of interest

 

 

Upvote
Downvote
Loading...

Input

0x01F0

Expected Output

31