Set the Rightmost Unset Bit

You are given a positive integer n.

Your task is to set the rightmost unset bit (0 bit) in the binary representation of n.

Setting a bit means changing it from 0 to 1. Return the resulting integer after performing the operation.

Example 1:

Input: n = 10

Output: 11

Explanation: 10 is 1010 in binary. The rightmost 0 bit is set to 1, producing 1011, which is 11.

Example 2:

Input: n = 13

Output: 15

Explanation: 13 is 1101. Its rightmost unset bit is changed to 1: 1101 -> 1111. Therefore, the result is 15.

Now Your Turn!

Pick the correct output for the given input

Input: n = 8

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n <= 109
0
class Solution {
public:
int setRightmostUnsetBit(int n) {
// Your code goes here
return 0;
}
};
Test Case

Input:

N