#include <stdio.h>
#include <stdint.h>
#define MASK1(register) (register & 0x00007C00)
#define MASK2(register) (register & 0xFFFF83FF)
#define LIMIT 31U
uint32_t update_register(uint32_t reg) {
// Your logic here
uint32_t modified_reg1;
uint32_t modified_reg2;
uint32_t result;
modified_reg1 = MASK1(reg);
uint32_t check_value = modified_reg1 >> 10;
if(check_value < LIMIT)
{
check_value += 1;
}
modified_reg2 = MASK2(reg);
result = modified_reg2 | (check_value << 10);
return result;
}
int main() {
uint32_t reg;
scanf("%u", ®);
uint32_t updated = update_register(reg);
printf("%u", updated);
return 0;
}
input is a 32 bit register
function needs to check the value of bits 10 - 14
if the value is less than 31 then increment it by 1
this value should be updated in the same position
we will need a few masks
1. Get the 5 bits from the desired location
0000_0000_0000_0000
0111_1100_0000_0000
2^6+2^5+2^4+2^3+2^2
64+32+16+8+4
mask1 -> 0x00007C00
mask2 -> 0xFFFF83FF
2. shift the masked value to the right by 10
3. check the value
4. shift it back to the left by 10
5. mask2 or this shifted value
Input: 0x00003C00
0000_0000_0000_0000
0011_1100_0000_0000
how is bits 10-14 0x1E
if the above masked value is moved to the right by 7 then it becomes
01_1110 -> 0x1E
Input
15360
Expected Output
16384