Extract Status Flags from a Hardware Register

Quick Overview

Extract three status flags and three multi-bit fields from a 32-bit hardware status register with shifts and masks. Tests bit numbering from the least significant bit, building a mask for a field of a given width, and ignoring bits that sit outside the layout.

Extract Status Flags from a Hardware Register

Company: Spacex

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a 32-bit integer `register` representing a hardware status register, extract specific status flags from it using bitwise operations. The register layout is defined as follows: - Bit 0: `READY` flag (1 bit, value 0 or 1) - Bit 1: `ERROR` flag (1 bit, value 0 or 1) - Bits 2-4: `MODE` value (3 bits, value 0-7) - Bit 5: `OVERFLOW` flag (1 bit, value 0 or 1) - Bits 6-7: `PRIORITY` value (2 bits, value 0-3) - Bits 8-15: `DATA` value (8 bits, value 0-255) Return a list of 6 integers, `[ready, error, mode, overflow, priority, data]`, extracted from the register. ### Function Signature ```python def extract_flags(register: int) -> list[int]: ``` ### Rules - Bit 0 is the least significant bit. - A multi-bit field is read as an unsigned number whose lowest bit is the field's lowest bit position. For example, `MODE` is `bit2 + 2 * bit3 + 4 * bit4`. - Bits 16-31 belong to no field and never change the result. - The output order is always `[ready, error, mode, overflow, priority, data]`. ### Constraints - `0 <= register <= 2^32 - 1` ### Examples ```text Input: register = 65535 Output: [1, 1, 7, 1, 3, 255] ``` 65535 is `0x0000FFFF`, so bits 0-15 are all 1. Each flag is 1 and each multi-bit field holds its largest value: `MODE` is 7, `PRIORITY` is 3 and `DATA` is 255. ```text Input: register = 43981 Output: [1, 0, 3, 0, 3, 171] ``` 43981 is `0xABCD`. The low byte `0xCD` is `11001101` in binary, so bit 0 is 1, bit 1 is 0, bits 4 down to 2 read `011` (3), bit 5 is 0 and bits 7 down to 6 read `11` (3), while the high byte `0xAB` gives `DATA = 171`. ```text Input: register = 4294901760 Output: [0, 0, 0, 0, 0, 0] ``` 4294901760 is `0xFFFF0000`, so only bits 16-31 are set. None of them is part of a field, so every value is 0.

Overview: Extract three status flags and three multi-bit fields from a 32-bit hardware status register with shifts and masks. Tests bit numbering from the least significant bit, building a mask for a field of a given width, and ignoring bits that sit outside the layout.

Spacex logo
Spacex
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a 32-bit integer register representing a hardware status register, extract specific status flags from it using bitwise operations.

The register layout is defined as follows:

  • Bit 0: READY flag (1 bit, value 0 or 1)
  • Bit 1: ERROR flag (1 bit, value 0 or 1)
  • Bits 2-4: MODE value (3 bits, value 0-7)
  • Bit 5: OVERFLOW flag (1 bit, value 0 or 1)
  • Bits 6-7: PRIORITY value (2 bits, value 0-3)
  • Bits 8-15: DATA value (8 bits, value 0-255)

Return a list of 6 integers, [ready, error, mode, overflow, priority, data], extracted from the register.

Function Signature

def extract_flags(register: int) -> list[int]:

Rules

  • Bit 0 is the least significant bit.
  • A multi-bit field is read as an unsigned number whose lowest bit is the field's lowest bit position. For example, MODE is bit2 + 2 * bit3 + 4 * bit4 .
  • Bits 16-31 belong to no field and never change the result.
  • The output order is always [ready, error, mode, overflow, priority, data] .

Constraints

  • 0 <= register <= 2^32 - 1

Examples

Input: register = 65535
Output: [1, 1, 7, 1, 3, 255]

65535 is 0x0000FFFF, so bits 0-15 are all 1. Each flag is 1 and each multi-bit field holds its largest value: MODE is 7, PRIORITY is 3 and DATA is 255.

Input: register = 43981
Output: [1, 0, 3, 0, 3, 171]

43981 is 0xABCD. The low byte 0xCD is 11001101 in binary, so bit 0 is 1, bit 1 is 0, bits 4 down to 2 read 011 (3), bit 5 is 0 and bits 7 down to 6 read 11 (3), while the high byte 0xAB gives DATA = 171.

Input: register = 4294901760
Output: [0, 0, 0, 0, 0, 0]

4294901760 is 0xFFFF0000, so only bits 16-31 are set. None of them is part of a field, so every value is 0.