2 ms·
After reading the title of the article, I tried to solve it for fun before looking up the solution. The logic followed what I learned at school about how to do
by ikatson 4y ago
After reading the title of the article, I tried to solve it for fun before looking up the solution.
The logic followed what I learned at school about how to do it the long way, but of course with binary numbers it's so much easier.
I ended up with a shorter implementation that follows the same idea, but uses recursion [1]. The recursion is compiled away by Rust [2]
[1] https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ab374bcbd751432d25df325d6dfb3f6f https://play.rust-lang.org/?version=stable&mode=debug&editio...
[2] https://godbolt.org/z/bq3fnrE4h https://godbolt.org/z/bq3fnrE4h