#include <stdio.h> #include <stdint.h> unsigned char extractNibble(unsigned char reg, int pos) { if (pos != 0 && pos != 1) return -1; uint8_t shift_val; pos == 1 ? shift_val = 4 : shift_val = 0; uint8_t mask = 0x0F; // mask to get all values in a nibble reg &= (mask << shift_val); return reg >> shift_val;; } int main() { unsigned char reg; int pos; scanf("%hhu %d", ®, &pos); printf("%d", extractNibble(reg, pos)); return 0; }
Test Cases
Test Results
Input
170 0
Expected Output
10