Skip to content

Commit e1adcc7

Browse files
committed
reverse-bits
1 parent ce305d8 commit e1adcc7

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

reverse-bits/DaleSeo.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// TC: O(1)
2+
// SC: O(1)
3+
impl Solution {
4+
pub fn reverse_bits(n: i32) -> i32 {
5+
let mut result = 0u32;
6+
let mut num = n as u32;
7+
8+
for i in 0..32 {
9+
// Extract the least significant bit
10+
let bit = num & 1;
11+
// Place it in the reversed position
12+
result |= bit << (31 - i);
13+
// Shift num right to process the next bit
14+
num >>= 1;
15+
}
16+
17+
result as i32
18+
}
19+
}

0 commit comments

Comments
 (0)