In many embedded microcontrollers (e.g., STM32, AVR), hardware interrupt flags in Status Registers operate on a "Write-1-to-Clear" (W1C) logic.
1 to that bit position.0 to a bit has no effect.A common bug occurs when developers use standard "Read-Modify-Write" logic (e.g., reg &= ~mask) to clear flags. This is dangerous because reading the register might capture other pending flags, and writing them back as 1 (if the read value is preserved) will accidentally clear those unhandled interrupts.
Your task is to encapsulate this logic in a StatusRegister class:
clearFlag method that accepts a bit_index.1 only at the specified position.busWrite() function.&= ~) operators.Program Flow:
StatusRegister instance.N (number of operations).N times:bit_index (the flag to clear).driver.clearFlag(bit_index).busWrite function (internal simulation) prints the value written to the bus.Input Format:
N (number of test inputs).N lines: Integer bit_index (0 to 31).Output Format:
busWrite function prints the hex value being written.Write: 0x<8-digit-HEX>Example:
Example 1
Input:
3
0
4
31Output:
Write: 0x00000001
Write: 0x00000010
Write: 0x80000000
Constraints:
N range: 1 to 20bit_index range: 0 to 31|=, &=)
Input
3 0 4 31
Expected Output
Write: 0x00000001 Write: 0x00000010 Write: 0x80000000