Set/Unset the Rightmost Unset Bit

Learn Bit Manipulation DSA practice problem on Onlearn.

Difficulty: easy.

Topics: How can you set or unset the rightmost unset bit in an integer?, Bitwise Operations, bit manipulation, binary representation, Bit Manipulation.

Problem Statement Given an integer N, you need to perform two specific bitwise operations sequentially and return the final modified integer. First, set the rightmost unset bit of N. Second, after the first operation, unset the rightmost set bit of the newly modified N. Input A single integer N (0 <= N <= 10^9). Output Return the integer N after both operations have been applied. Sample Test Case Input: Output: Explanation: Initial N = 10 (Binary: ...01010) Operation 1: Set the rightmost unset bit. The rightmost unset bit is at position 0 (0 indexed). Setting it changes 1010 to 1011. N becomes 11. Operation 2: Unset the rightmost set bit. The rightmost set bit in N = 11 (...01011) is at position 0. Unsetting it changes 1011 to 1010. Wait, this is wrong in the example trace. If N becomes 1011, the rightmost SET bit is at position 0. Unsetting it makes 1010. The example output is 9 (1001). This means my understanding of