All submissions

Extract Bit Field from 16-bit Register

Code

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

uint8_t extract_field(uint16_t reg) {
    // Your logic here
    // 1. Define the start bit and field width for clarity.
    const uint8_t START_BIT = 4;
    const uint8_t FIELD_WIDTH = 5;
const uint16_t MASK = ((1U << FIELD_WIDTH) - 1U) << START_BIT;
uint16_t isolated_field = reg & MASK;
  uint8_t field_value = isolated_field >> START_BIT;

    return field_value;
}


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

Solving Approach

 

 

 

Loading...

Input

0x01F0

Expected Output

31