Extract the bits from position i to j of an integer using a bitmask

Quick Overview

Extract the bits of a 32-bit integer from position i to position j and return them as a number whose lowest bit is the original bit i. It tests building a bitmask of a given width, combining shifts with AND, and handling the inclusive range and the full 32-bit field without overflow.

Extract the bits from position i to j of an integer using a bitmask

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a 32-bit integer `num` and two integers `i` and `j` representing bit positions (0-indexed from the least significant bit), create a bitmask that isolates the bits from position `i` to position `j` (inclusive) and return the extracted value. Specifically, extract the bits of `num` from position `i` to position `j`, shift them down so that bit `i` becomes bit `0`, and return the resulting integer. This operation is common in hardware register manipulation, where specific bit fields within a register hold different configuration values that need to be read or written independently. ### Function Signature ```python def extract_bits(num: int, i: int, j: int) -> int: ``` ### Rules - Bit `0` is the least significant bit of `num`. - The result is the non-negative value of the field: bit `i` of `num` becomes bit `0` of the result, bit `i + 1` becomes bit `1`, and so on up to bit `j`. Every bit of the result above position `j - i` is `0`. ### Constraints - `0 <= num <= 2^31 - 1` - `0 <= i <= j <= 31` ### Examples ```text Input: num = 182, i = 2, j = 5 Output: 13 ``` 182 in binary is `10110110`. Reading from bit 5 down to bit 2 gives `1101`, and shifting that field down to position 0 gives `0b1101 = 13`. ```text Input: num = 255, i = 0, j = 3 Output: 15 ``` 255 in binary is `11111111`, so bits 0 to 3 are `1111`. The field already starts at bit 0, and its value is `0b1111 = 15`. ```text Input: num = 2147483647, i = 16, j = 31 Output: 32767 ``` 2147483647 is `2^31 - 1`, so bits 0 to 30 are set and bit 31 is `0`. The 16-bit field from bit 16 to bit 31 holds fifteen 1-bits under a 0, which is `2^15 - 1 = 32767`.

Overview: Extract the bits of a 32-bit integer from position i to position j and return them as a number whose lowest bit is the original bit i. It tests building a bitmask of a given width, combining shifts with AND, and handling the inclusive range and the full 32-bit field without overflow.

Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a 32-bit integer num and two integers i and j representing bit positions (0-indexed from the least significant bit), create a bitmask that isolates the bits from position i to position j (inclusive) and return the extracted value.

Specifically, extract the bits of num from position i to position j, shift them down so that bit i becomes bit 0, and return the resulting integer.

This operation is common in hardware register manipulation, where specific bit fields within a register hold different configuration values that need to be read or written independently.

Function Signature

def extract_bits(num: int, i: int, j: int) -> int:

Rules

  • Bit 0 is the least significant bit of num .
  • The result is the non-negative value of the field: bit i of num becomes bit 0 of the result, bit i + 1 becomes bit 1 , and so on up to bit j . Every bit of the result above position j - i is 0 .

Constraints

  • 0 <= num <= 2^31 - 1
  • 0 <= i <= j <= 31

Examples

Input: num = 182, i = 2, j = 5
Output: 13

182 in binary is 10110110. Reading from bit 5 down to bit 2 gives 1101, and shifting that field down to position 0 gives 0b1101 = 13.

Input: num = 255, i = 0, j = 3
Output: 15

255 in binary is 11111111, so bits 0 to 3 are 1111. The field already starts at bit 0, and its value is 0b1111 = 15.

Input: num = 2147483647, i = 16, j = 31
Output: 32767

2147483647 is 2^31 - 1, so bits 0 to 30 are set and bit 31 is 0. The 16-bit field from bit 16 to bit 31 holds fifteen 1-bits under a 0, which is 2^15 - 1 = 32767.