Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Hardware Register Bit Operations

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Hardware Register Bit Operations. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

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.

Task

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.

Constraints

  • 1 <= width <= 32
  • 0 <= register < 2^width
  • 1 <= len(operations) <= 10^5
  • Each operation is one of set, clear, toggle, read
  • 0 <= bit < width

Function Signature

def process_register_ops(register, width, operations):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output