1299. Clear All Bits from MSB to Nth Bit

EasyBit ManipulationBit ManipulationBit MaskingClear

Given a non-negative integer n and a 0-indexed position i, clear every bit from the most-significant bit down to bit i inclusive, keeping only bits 0..i-1. When i is 0 the result is 0.

Input: A JSON object {"n": <non-negative integer>, "i": <bit index>} with 0 <= i <= 50.

Output: Return n with bits i and above cleared.

Examples

Example 1
Input: {"n":26,"i":3}
Output: 2
Explanation: Keep bits 0..2 of 11010 -> 010 = 2.
Example 2
Input: {"n":26,"i":0}
Output: 0
Explanation: Clearing from the top through bit 0 leaves 0.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →