All submissions

Extract Bit Field from 16-bit Register

Code

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

// Define macros here
#define MASK(BITS) ((1U << BITS)-1)
#define SHIFTED_MASK(POS, BITS) ((MASK(BITS)) << POS)
#define CLEAR_BITS(REG, POS, BITS)  \
((REG) &= ~SHIFTED_MASK(POS, BITS))
#define SET_BITS(REG, POS, BITS, VAL)   \
((REG) = ((REG) & ~SHIFTED_MASK(POS, BITS)) | (((VAL) & MASK(BITS)) << (POS)) )
#define GET_BITS(REG, POS, BITS)    \
(((REG) >> (POS)) & MASK(BITS))


uint8_t extract_field(uint16_t reg) {
    // Your logic here
    uint8_t res =  (uint8_t) GET_BITS(reg, 4, 5);
    return res;
}

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

Solving Approach

 

 

 

Loading...

Input

0x01F0

Expected Output

31