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
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.