1313. Toggle All Bits After MSB

EasyBit ManipulationBit ManipulationXORMSB

Given a non-negative integer n, flip every bit below the most-significant set bit while keeping that top bit unchanged. If n is 0 there is no set bit, so return 0.

Input: A JSON object {"n": <non-negative integer>}.

Output: Return n with all bits below its MSB toggled.

Examples

Example 1
Input: {"n":22}
Output: 25
Explanation: Keep bit 4, flip bits 0..3 of 10110 -> 11001 = 25.
Example 2
Input: {"n":8}
Output: 15
Explanation: 1000 keeps its top bit and flips the rest -> 1111 = 15.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →