1300. Clear All Bits from Nth Bit to LSB

EasyBit ManipulationBit ManipulationBit MaskingClear

Given a non-negative integer n and a 0-indexed position i, clear every bit from bit i down to the least-significant bit inclusive, keeping only bits above i.

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

Output: Return n with bits 0..i cleared.

Examples

Example 1
Input: {"n":26,"i":2}
Output: 24
Explanation: Clear bits 0..2 of 11010 -> 11000 = 24.
Example 2
Input: {"n":26,"i":0}
Output: 26
Explanation: Clear only bit 0 -> 11010 = 26 (bit 0 was already 0).

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →