All submissions

Extract the Nibble from an 8-bit Register

Code

#include <stdio.h>

int main() {
     char reg,nibble; // 8-bit register value
    int pos;           // nibble position (0 = lower, 1 = upper)


    // Input: 8-bit register value and nibble position
    scanf("%hu %d", &reg, &pos);

    if (pos == 0) {
        // Lower nibble (bits 0-3)
        nibble = reg & 0x0F;  // Mask 00001111
    } 
    else if (pos == 1) {
        // Upper nibble (bits 4-7)
        nibble = (reg >> 4) & 0x0F; // Shift right, mask
    } 
    else {
        printf("Invalid nibble position\n");
        return 0;
    }

    // Output decimal value of nibble
    printf("%u\n", nibble);
    return 0;
}

Solving Approach

 

 

 

Loading...

Input

170 0

Expected Output

10