Extract Bit Field from 16-bit Register

Code

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

uint8_t extract_field(uint16_t reg) {
    // Your logic here
    struct hello{
       unsigned int a  :4;
      unsigned int b   :5;
       unsigned int c  :7;

    }__attribute__((__packed__));
struct hello * me=(struct hello *)&reg;
    return me->b;
}

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

0x01F0

Expected Output

31