Right propagate the rightmost 1-bit
EasyUpdated: Sep 18, 2025
Problem
Given an integer x, right-propagate the rightmost set bit: set all bits to the right of the rightmost 1 (inclusive) to 1, leaving higher-order bits unchanged. Formally return x | (x - 1).
Examples
Example 1
Input: x = 0b01010000
Output: 0b01011111
Example 2
Input: x = 0b00000000
Output: 0b11111111 # behavior on unsigned underflow; see notes
Solution
Method 1 — Bit trick (x | (x - 1))
Intuition
- Subtracting
1fromxclears the rightmost1bit and sets all lower bits to1(they were zeros). OR-ing with the originalxtherefore preserves higher-order bits and turns all lower bits (including the rightmost1) into1.
Approach
- If
x == 0, note thatx - 1underflows in unsigned arithmetic and will be all ones;x | (x - 1)will therefore produce all ones. - Otherwise compute and return
ans = x | (x - 1).
Code
C++
class Solution {
public:
unsigned int rightPropagate(unsigned int x) {
return x | (x - 1u);
}
};
Go
package main
func rightPropagate(x uint32) uint32 {
return x | (x - 1)
}
Java
class Solution {
public int rightPropagate(int x) {
return x | (x - 1);
}
}
Python
class Solution:
def right_propagate(self, x: int) -> int:
return x | (x - 1)
Complexity
- ⏰ Time complexity:
O(1), single arithmetic and bitwise operations. - 🧺 Space complexity:
O(1).
Method 2 — Find rightmost set bit then apply mask
Intuition
- Locate the index
posof the rightmost set bit and construct a mask ofposlower bits set to1((1 << pos) - 1). OR this mask withxto turn on all lower bits.
Approach
- If
x == 0, return~0for unsigned underflow behavior or0if you prefer a guarded return. - Find
possuch that(x >> pos) & 1 == 1and(x & ((1 << pos) - 1)) == 0(scan LSB upward). - Compute
mask = (1u << pos) - 1uand returnx | mask.
Edge cases:
- This method avoids relying on underflow semantics if you prefer explicit behavior for
x == 0.
Code
C++
class Solution {
public:
unsigned int rightPropagate(unsigned int x) {
if (x == 0) return ~0u; // or return 0 if guarded behavior desired
unsigned int pos = 0;
unsigned int tmp = x;
while ((tmp & 1u) == 0u) {
tmp >>= 1;
++pos;
}
unsigned int mask = (1u << pos) - 1u;
return x | mask;
}
};
Go
package main
func rightPropagate(x uint32) uint32 {
if x == 0 {
return ^uint32(0) // or 0 for guarded behavior
}
pos := uint32(0)
tmp := x
for (tmp & 1) == 0 {
tmp >>= 1
pos++
}
mask := (uint32(1) << pos) - 1
return x | mask
}
Java
class Solution {
public int rightPropagate(int x) {
if (x == 0) return ~0; // or 0 if you prefer
int pos = 0;
int tmp = x;
while ((tmp & 1) == 0) {
tmp >>>= 1;
pos++;
}
int mask = (1 << pos) - 1;
return x | mask;
}
}
Python
class Solution:
def right_propogate(self, x: int) -> int:
if x == 0:
return ~0 # or return 0 for guarded behavior
pos = 0
tmp = x
while (tmp & 1) == 0:
tmp >>= 1
pos += 1
mask = (1 << pos) - 1
return x | mask
Complexity
- ⏰ Time complexity:
O(w)wherewis the word size (number of bits) due to scanning bits; in practiceO(1)for fixed-width integers. - 🧺 Space complexity:
O(1).
Notes
- The canonical one-line trick is
x | (x - 1)(Method 1). Be aware that whenx == 0this expression produces all1bits due to unsigned underflow; choose guarded behavior if that is undesirable. - Reference: original bit-hacks collection in
source_links.