17. Extract the Nibble from an 8-bit Register

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>

unsigned char extractNibble(unsigned char reg, int pos) {
    // Write your code here
    unsigned char ans;
    if (pos == 0)
        ans = reg & 0x0Fu;          // lower nibble
    else
        ans = (reg >> 4) & 0x0Fu;   // upper nibble
    return ans;
}

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