Rust Day 18: Leetcode — Number of 1 Bit
Published in
2 min readMay 8, 2022
Today, I moved out to a new place. The entire day was spent mostly shifting. Couldn’t focus much on the DB side of things. Anyways, completing the LC streak.
This is a well-known problem called hamming distance.
Problem:
Logic:
The above logic is used in the Fenwick tree.
This is more like scanning every 32 bits of the integer and checking if it is set to 1 or not.
Code:
Syntax:
for i in 0..32
: for loop syntax(n&1) as i32
: gets the last bit and casts it to i32 for adding it.
Reference:
- https://leetcode.com/problems/number-of-1-bits/discuss/1648157/Rust-one-liner-popcnt-solution
- https://leetcode.com/problems/number-of-1-bits/discuss/1337476/Rust-Bit-MaskManipulation
Found it Interesting?
Please show your support by 👏