Extract the Nibble from an 8-bit Register

Code

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

unsigned char extractNibble(unsigned char reg, int pos){
    unsigned char output; 
    // pos = 1 -> upper, pos = 0 -> lower 
    if(pos)
    {
        output = (reg >> 4) & 0x0F;
    }       
    else
    {
        output = reg & 0x000F; 
    }
    return output; 
}

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

170 0

Expected Output

10