From e1adcc72cf8c938d8192d94492c5c7bd6d4882f6 Mon Sep 17 00:00:00 2001 From: Dale Seo Date: Mon, 3 Nov 2025 21:57:11 -0500 Subject: [PATCH] reverse-bits --- reverse-bits/DaleSeo.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 reverse-bits/DaleSeo.rs diff --git a/reverse-bits/DaleSeo.rs b/reverse-bits/DaleSeo.rs new file mode 100644 index 000000000..3b6d272f9 --- /dev/null +++ b/reverse-bits/DaleSeo.rs @@ -0,0 +1,19 @@ +// TC: O(1) +// SC: O(1) +impl Solution { + pub fn reverse_bits(n: i32) -> i32 { + let mut result = 0u32; + let mut num = n as u32; + + for i in 0..32 { + // Extract the least significant bit + let bit = num & 1; + // Place it in the reversed position + result |= bit << (31 - i); + // Shift num right to process the next bit + num >>= 1; + } + + result as i32 + } +}