Extract the Nibble from an 8-bit Register

Code

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

#define mask 0x0F

uint8_t extracNibble(uint8_t reg, int pos){
    if (pos == 0){
        return reg  &= mask;
    }
    else if(pos == 1){
        return reg = (reg >> 4) &mask;
    }
}


int main() {
    uint8_t pos;
    uint8_t reg;
    scanf("%hhu %hhu", &reg, &pos);
    printf("%d", extracNibble(reg, pos));
    return 0;
} 

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

170 0

Expected Output

10