In embedded code for Google Pixel device drivers, low-level register access often requires changing individual bits without affecting the rest of the register. Implement a function that processes a sequence of bit operations on an unsigned register value.
Write a function that takes an initial integer register, a register width width, and a list of operations. Each operation is one of:
("set", bit)("clear", bit)("toggle", bit)("read", bit)For each read operation, record whether the target bit is 0 or 1. Return the final register value and the list of read results.
Bit positions are 0-indexed from the least significant bit. All updates must stay within the lower width bits.
register (int), width (int), operations (list of [op, bit] pairs)[final_register, read_results] where read_results is a list of integers 0 or 1Example 1
register = 10, width = 4, operations = [["set", 0], ["clear", 3], ["toggle", 1], ["read", 1]][1, [0]]10 is 1010. After set bit 0 -> 1011, clear bit 3 -> 0011, toggle bit 1 -> 0001, read bit 1 -> 0.Example 2
register = 0, width = 8, operations = [["set", 7], ["read", 7], ["toggle", 7], ["read", 7]][0, [1, 0]]1, toggled off, and read as 0.1 <= width <= 320 <= register < 2^width1 <= len(operations) <= 10^5set, clear, toggle, read0 <= bit < width