Keep the Low Bits
Return the number made of only the lowest k bits of n. All higher bits are thrown away.
For example 45 is 101101 in binary. Its lowest 3 bits are 101, so with k = 3 the answer is 5. With k = 0 no bits are kept and the answer is 0.
Example 1
n 45
k 3
returns 5
45 is 101101. The lowest three bits are 101.
Example 2
n 45
k 4
returns 13
The lowest four bits of 101101 are 1101.
Example 3
n 6
k 10
returns 6
Keeping more bits than the number has changes nothing.
Constraints
0 <= n <= 10**90 <= k <= 30
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Bits problems
- Lowest Set BitBeginner
- Parity BitBeginner
- Read One BitBeginner
- Read a Binary StringBeginner
- Set Bit PositionsBeginner
- Switch Off the Lowest OneBeginner
- Add Without PlusEasy
- Alternating BitsEasy