All submissions

Extract and Modify Field in a 32-bit Register

Code

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

#define BIT_FIELD_LIMIT 31
#define BIT_FIELD_START_POSITION 10
#define FIELD_BIT_MASK 0x1F
uint32_t update_register(uint32_t reg) {
    uint32_t field;
    field = (reg>>BIT_FIELD_START_POSITION)&FIELD_BIT_MASK;
    if(field<BIT_FIELD_LIMIT)
    {
        field++;
        reg&=(uint32_t)(~(FIELD_BIT_MASK<<BIT_FIELD_START_POSITION)); // reset bit field
        reg|=(field<<BIT_FIELD_START_POSITION);

    }
    return reg;
}

int main() {
    uint32_t reg;
    scanf("%u", &reg);
    uint32_t updated = update_register(reg);
    printf("%u", updated);
    return 0;
}

Solving Approach

 

 

 

Loading...

Input

15360

Expected Output

16384