All submissions

Extract the Nibble from an 8-bit Register

Code

#include <stdio.h>

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

    unsigned char nibble;

    if (pos == 0) {
        // Lower nibble
        nibble = reg & 0x0F;
    } else {
        // Upper nibble
        nibble = (reg >> 4) & 0x0F;
    }

    printf("%d\n", nibble);
    return 0;
}

Solving Approach

 

 

 

Loading...

Input

170 0

Expected Output

10