27. Extract Bit Field from 16-bit Register

Back To All Submissions
Previous Submission
Next Submission

Code

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

uint8_t extract_field(uint16_t reg) {
    // Your logic here
    uint16_t mask = ((1 << 5) - 1) << 4;
    return (reg & mask) >> 4;
}

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

Solving Approach

Extract the bit field using a mask of 5 set bits. Apply AND operator bitwise and right shift the result to the LSB.

 

 

Was this helpful?
Upvote
Downvote