Build a bitmask with bits lo through hi set and return it as a hex string

Quick Overview

Build an integer whose bits from index lo to index hi are set to one and return it as a lowercase hexadecimal string with a 0x prefix. The problem tests shift and subtraction tricks for bit ranges, inclusive off-by-one handling, and overflow pitfalls when all 32 bits are set.

Build a bitmask with bits lo through hi set and return it as a hex string

Company: NVIDIA

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Create a bitmask where the bits from index `lo` to `hi` (inclusive) are set to `1` and all other bits are `0`. Bit indices are 0-based, and index `0` is the least significant bit. Return the result as a hexadecimal string prefixed with `0x`. ### Function Signature ```python def create_bitmask(lo: int, hi: int) -> str: ``` ### Rules - Bit `k` has the value `2^k`, so bit 0 is the least significant bit. Both `lo` and `hi` are included in the range. - After the `0x` prefix, write the value in lowercase hexadecimal digits with no leading zeros. This is the format Python's `hex()` produces, for example `0x7c`, never `0x7C` or `0x0000007c`. - Since `lo <= hi`, at least one bit is always set, so the result is never `0x0`. ### Constraints - `0 <= lo <= hi <= 31` ### Examples ```text Input: lo = 2, hi = 6 Output: 0x7c ``` Bits 2, 3, 4, 5 and 6 are set, which is 0b1111100 in binary. That equals 4 + 8 + 16 + 32 + 64 = 124 in decimal, or 0x7c in hexadecimal. ```text Input: lo = 0, hi = 7 Output: 0xff ``` Bits 0 through 7 are all set, which is 0b11111111 in binary, or 255 in decimal. ```text Input: lo = 0, hi = 31 Output: 0xffffffff ``` All 32 bits are set, giving the value 4294967295 (two to the 32nd power, minus 1). Each hex digit covers four bits, so the answer is eight `f` digits.

Overview: Build an integer whose bits from index lo to index hi are set to one and return it as a lowercase hexadecimal string with a 0x prefix. The problem tests shift and subtraction tricks for bit ranges, inclusive off-by-one handling, and overflow pitfalls when all 32 bits are set.

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

Create a bitmask where the bits from index lo to hi (inclusive) are set to 1 and all other bits are 0. Bit indices are 0-based, and index 0 is the least significant bit.

Return the result as a hexadecimal string prefixed with 0x.

Function Signature

def create_bitmask(lo: int, hi: int) -> str:

Rules

  • Bit k has the value 2^k , so bit 0 is the least significant bit. Both lo and hi are included in the range.
  • After the 0x prefix, write the value in lowercase hexadecimal digits with no leading zeros. This is the format Python's hex() produces, for example 0x7c , never 0x7C or 0x0000007c .
  • Since lo <= hi , at least one bit is always set, so the result is never 0x0 .

Constraints

  • 0 <= lo <= hi <= 31

Examples

Input: lo = 2, hi = 6
Output: 0x7c

Bits 2, 3, 4, 5 and 6 are set, which is 0b1111100 in binary. That equals 4 + 8 + 16 + 32 + 64 = 124 in decimal, or 0x7c in hexadecimal.

Input: lo = 0, hi = 7
Output: 0xff

Bits 0 through 7 are all set, which is 0b11111111 in binary, or 255 in decimal.

Input: lo = 0, hi = 31
Output: 0xffffffff

All 32 bits are set, giving the value 4294967295 (two to the 32nd power, minus 1). Each hex digit covers four bits, so the answer is eight f digits.