Extract a Register Bit Field
Problem statement
An unsigned 32-bit register contains the integer value. Bit positions are numbered from zero at the least significant bit.
Extract the width consecutive bits beginning at offset, shift that field down so its lowest bit becomes bit zero, and return its unsigned numeric value. Do not change the register value.
Function
extractRegisterBits(value: long, offset: int, width: int) → longExamples
Example 1
value = 42offset = 1width = 3return = 5Binary 101010 shifted right once has low three bits 101, whose value is five.
Example 2
value = 4294967295offset = 0width = 32return = 4294967295Selecting the full register preserves all thirty-two one bits.
Constraints
0 <= value <= 4294967295.0 <= offset <= 31.1 <= width <= 32andoffset + width <= 32.- The interface uses a signed 64-bit type to represent the full unsigned 32-bit range.