#include <stdio.h> unsigned char extractNibble(unsigned char reg, int pos) { // Write your code here switch (pos) { case 0: return (0x0F & reg); case 1: default: return (reg >> 4); } } int main() { unsigned char reg; int pos; scanf("%hhu %d", ®, &pos); printf("%d", extractNibble(reg, pos)); return 0; }
for the lower nibble you clear the top nibble by "bitwise and" with 0x0F
to get the upper nibble, shift right four bits
Test Cases
Test Results
Input
170 0
Expected Output
10