Extract and Modify Field in a 32-bit Register

Code

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

#define BIT_MASK     0x1F
#define BIT_POSITION 10

uint32_t update_register(uint32_t reg) {
    // Your logic here

    //1. Extract bits 10 - 14
    uint8_t bits = (reg >> BIT_POSITION) & BIT_MASK;

    //2. If is less than 31, increase it by 1. 
    if (bits < 31) bits += 1;

    //3. Clear bits from the register
    reg &= ~(BIT_MASK << BIT_POSITION);

    //4. Set back bits to the register
    reg |= (bits << BIT_POSITION);

    return reg;
}

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

15360

Expected Output

16384