Extract a Register Bit Field
Updated
Problem statement
An unsigned 32-bit register contains the integer value. Number the bits starting at zero from the rightmost, lowest-value 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 = 5In binary, value is 101010. The field starts at the offset, so shift right once to drop the lowest bit. The width is three, so keep the lowest three bits of what is left: 101. That is five, so the answer is five.
Example 2
value = 4294967295offset = 0width = 32return = 4294967295The offset is zero and the width covers the whole register. All thirty-two bits are ones, and the field keeps every one of them. So the answer is the same as value.
Constraints
0 <= value <= 4294967295.0 <= offset <= 31.1 <= width <= 32andoffset + width <= 32.valueand the answer use a signed 64-bit type, so the whole unsigned 32-bit range fits.
More Apple Onsite Interview questions
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- FIFO Mutex Operation TraceONSITE INTERVIEW · Seen Oct 2026
- Implement Hit CounterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Parse Nested HTML into a TreeONSITE INTERVIEW · Seen Oct 2026
- Thread-Safe Concurrent Linked ListONSITE INTERVIEW · Seen Sep 2026
- Palindrome Match PercentageONSITE INTERVIEW · Seen Sep 2026
- Binary Tree Nodes at Distance KONSITE INTERVIEW · Seen Aug 2026
- BST to Sorted Circular Doubly Linked ListONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Find the Missing Number from 1 to nHyperVerge · ONSITE INTERVIEW
- Missing and Repeated NumberCommvault · ONSITE INTERVIEWCommvault interview questions
- Check Endianness Of A Byte ArrayNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Swap Two Integers Without a Temporary VariableOracle · ONSITE INTERVIEWOracle interview questions
- Swap Two Numbers Without a Third VariableInfosys · ONSITE INTERVIEWInfosys interview questions
- Add Two Binary StringsPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
- Add BinaryMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Count Set Bits in an Integer ArrayAkuna Capital · ONSITE INTERVIEWAkuna Capital interview questions
- Check Whether a Byte Is NonzeroZscaler · ONSITE INTERVIEWZscaler interview questions
- Find the Odd-Occurrence ValueExpedia · ONSITE INTERVIEWExpedia interview questions