Extract lowest bits

Write codeEasy10 min

Your task

A device packs several fields into one non-negative integer, and the lowest k bits hold a rolling counter. Given the packed value n (0 <= n < 2^31) and a width k (0 <= k <= 30), return the counter: the integer formed by the lowest k bits of n. When k is 0 the counter is empty, so return 0. For example, n = 173 (binary 10101101) with k = 4 returns 13 (binary 1101).

Examples

low_bits_value(n: int, k: int) -> int
Example 1
Inputlow_bits_value(173, 4)
Output13
Example 2
Inputlow_bits_value(37, 5)
Output5