#include <stdio.h>
unsigned char extractNibble(unsigned char reg, int pos) {
// Write your code here
// checking if user is asking for lower nibble
// creating a 4-bit mask
unsigned char mask = 0xF; // mask = 0000 1111
if ( pos == 0 )
{
// for ex, user inputs 0000 0001 = 1, now we will have to extract lower nibble 0001
// we can make use of AND operation so input variable will be anded with mask and whatever matches with 1 will
// return 1 else will return 0
return reg & mask;
}
else
{
// ( value to be shifted(the input value) >> position(4-bit to the right) )
return ( reg >> 4 ) & mask;
}
}
int main() {
unsigned char reg;
int pos;
scanf("%hhu %d", ®, &pos);
printf("%d", extractNibble(reg, pos));
return 0;
}
Input
170 0
Expected Output
10