FastPrepGenerate Bit Mask

Generate Bit Mask

NVIDIA logoNVIDIA● EasyINTERNOA

Problem statement

Build a 32-bit mask in which every bit from position lo through position hi, inclusive, is 1. Every other bit must be 0. Bit positions are zero-indexed, with position 0 as the least significant bit.

Complete set_mask(hi, lo) and return the mask as a hexadecimal string prefixed with 0x. Use lowercase hexadecimal digits and no leading zeroes after the prefix.

Function

set_mask(hi: int, lo: int) → String

Examples

Example 1

hi = 2lo = 0return = "0x7"

Bits 0, 1, and 2 are set. Their values sum to 1 + 2 + 4 = 7, so the result is 0x7.

Example 2

hi = 5lo = 3return = "0x38"

Bits 3, 4, and 5 are set. Their values sum to 8 + 16 + 32 = 56, which is 0x38.

Example 3

hi = 31lo = 31return = "0x80000000"

Only bit 31 is set. Its value is 2147483648, whose hexadecimal form is 0x80000000.

Constraints

  • 0 <= lo <= hi <= 31.

More NVIDIA problems

See NVIDIA hiring insights
Saved
public String set_mask(int hi, int lo) {
    // write your code here
}
hi2
lo0
expected"0x7"
Checking account…