All submissions

Extract and Modify Field in a 32-bit Register

Code

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

uint32_t update_register(uint32_t reg) 
{
    uint8_t mask=(reg>>10)&(0x1F); //Step-1:extracted data corresponding to 5bits(Bit10 to Bit14)
    if(mask <31)
    {
        mask++;
        reg&=~( ((1<<5)-1)<<10); //Step-2:clearing bit10 to bit14 in the input received to the function.
        reg=reg|(mask<<10);      //Step-3:Then set the required mask at appropriate 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