Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Embedded Coding With Bit Shifts

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

Your question is Embedded Coding With Bit Shifts. 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

Samsung Semiconductor Inc (US) firmware frequently manipulates fixed-width register values. Implement a function that rotates the bit pattern of a 32-bit integer to the right by n positions.

A right rotation moves the least significant bit to the most significant position, while shifting every other bit one position right. The operation must behave as if the value contains exactly 32 bits, even though Python integers have arbitrary precision.

Formal Specification

Implement rotate_right(value, n):

  • value is a signed 32-bit integer in the range [-2^31, 2^31 - 1].
  • n is a nonnegative integer. Rotations larger than 31 must wrap around modulo 32.
  • Return the rotated 32-bit bit pattern as an unsigned integer in the range [0, 2^32 - 1].
  • Do not convert the value to a string or use library rotation helpers.

Constraints

  • -2^31 <= value <= 2^31 - 1
  • 0 <= n <= 10^9
  • The result must be returned as an unsigned 32-bit integer
  • Use O(1) additional space

Function Signature

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