All submissions

Extract Bit Field from 16-bit Register

Bitwise right shift and using anding to get the bits

#include <stdio.h>
#include <stdint.h>

uint8_t extract_field(uint16_t reg) {
    // Your logic here
    uint16_t mask = (0x1f << 4);
    mask = reg & mask;

    return mask >> 4;
    //return (reg >> 4) & 0x1f;
}

int main() {
    uint16_t reg;
    scanf("%hx", &reg);
    printf("%u", extract_field(reg));
    return 0;
}

Solving Approach

 

 

 

Loading...

Input

0x01F0

Expected Output

31