#include <stdio.h>
unsigned char extractNibble(unsigned char reg, int pos) {
// Write your code here
if(pos==0)//lower nibble
{
return (reg & 0x0F);
}
else if(pos==1)//upper nibble
{
return ((reg>>4)&0x0F);
}
else
{
return 0;
}
}
int main() {
unsigned char reg;
int pos;
scanf("%hhu %d", ®, &pos);
printf("%d", extractNibble(reg, pos));
return 0;
}Use bit masking (& 0x0F) to extract the lower nibble and right-shift by 4 bits (>> 4) followed by masking to get the upper nibble.
Control the logic using pos: if pos == 0, return lower nibble; if pos == 1, return upper nibble.
Input
170 0
Expected Output
10