easy +10 pts

Isolate Rightmost Set Bit

Return a number containing only the least significant 1 bit of the input.

Write a function `isolate_rightmost_bit(n: int) -> int` that returns an integer whose binary representation has only the lowest set bit (rightmost 1) of `n` set to 1, and all other bits set to 0. For example, if `n = 12` (binary `1100`), the rightmost set bit is at position 2 (0-indexed from the right), so the result is `4` (binary `0100`). If `n` is 0, return 0. **Important:** - The function must work for both positive and negative integers. For negative numbers, Python uses two's complement representation with infinite leading 1s. The rightmost set bit is still the lowest 1 in that representation. For example, `isolate_rightmost_bit(-12)` should return `4`, because `-12` in two's complement has its least significant 1 at bit position 2. **Signature:** `def isolate_rightmost_bit(n: int) -> int:`

Constraints

Input is any Python integer (unbounded). The output must be a non-negative integer that is a power of two (or 0). Time complexity: O(1) bitwise operations.

Example

>>> isolate_rightmost_bit(12)
4
>>> isolate_rightmost_bit(0)
0
>>> isolate_rightmost_bit(7)
1
>>> isolate_rightmost_bit(16)
16
>>> isolate_rightmost_bit(-12)
4
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Consider the property `x & -x`. How does it behave?
The two's complement of a number flips bits and adds 1, which often isolates the lowest set bit when ANDed.
Test with small numbers like 1, 2, 3, 4 to see the pattern.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.