Cover image for LeetCode Daily Problem P3370: Smallest Integer with Only Set Bits

LeetCode Daily Problem P3370: Smallest Integer with Only Set Bits


Timeline

timeline

2025-10-29

init

bit manipulation

Problem:

Simple bit manipulation

1
2
3
4
5
6
7
8
9
10
11
12
13
struct Solution;
impl Solution {
pub fn smallest_number(n: i32) -> i32 {
let mut i = 1_i64;
while i < n as i64 {
i = (i << 1) | 1;
}
i as i32
}
}
fn main() {
println!("Hello, world!");
}