All submissions

Extract the Nibble from an 8-bit Register

Code

#include <stdio.h>
/*
This can be thought of a shifting problem
8 bit reg: 0000 0000
Extract either of the nibbles
if pos = 0
reg = reg & 0000 1111
if pos = 1
reg = reg & 1111 0000 
but then shift the upper nibble right by 4 bits

*/
unsigned char extractNibble(unsigned char reg, int pos) {
    // Write your code here
    unsigned char output = 0;
    if(pos == 0)
    {
        output = reg & 0x0F;
    }
    else
    {
        output = (reg & 0xF0) >> 4;
    }

    return output;
}

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

Solving Approach

 

 

 

Loading...

Input

170 0

Expected Output

10