Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Set Register Bit with Mask

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

Your question is Set Register Bit with Mask. 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 NXP Semiconductors embedded software, register updates are often done with bit masks. Given a 32-bit unsigned register value, write a function that sets the 5th bit and returns the updated register.

Assume bit positions are 0-indexed from the least significant bit, so the 5th bit means bit index 4.

Formal Specification

  • Input: An integer reg representing a 32-bit unsigned register value.
  • Output: An integer representing the updated 32-bit value after setting bit 4.
  • The operation must preserve all other bits.

Constraints

  • 0 <= reg <= 4294967295
  • The register is treated as a 32-bit unsigned value
  • Bit positions are 0-indexed from the least significant bit
  • The solution should use bitwise operations

Function Signature

def set_fifth_bit(reg):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output