17. Extract the Nibble from an 8-bit Register

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>

#define NIBBLE_MASK 0x0F
#define NIBBLE_LEN  4

unsigned char extractNibble(unsigned char reg, int pos) {
    // Write your code here
    unsigned char extracted = (reg >> (NIBBLE_LEN * pos)) & NIBBLE_MASK;
    return extracted;
}

int main() {
    unsigned char reg;
    int pos;
    scanf("%hhu %d", &reg, &pos);
    printf("%d", extractNibble(reg, pos));
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote