Reversing an n-bit number in O(log n) time
▻https://hackernoon.com/reversing-an-n-bit-number-in-o-log-n-time-9bf69363d452?source=rss----3a8
Reversing an n-bit number in O(log n)Last week, while writing my previous post, I came across an interesting algorithm for reversing an integer. For example, it takes 10100011 and transform it into 11000101. What’s cool about this algorithm, is that it does it in O(log n) iterations. This is the code:▻https://medium.com/media/36298d4e1cf4a2b30e7daf2c317543ea/hrefYes. 9 lines including signature and closing braces. Let’s see how it works.The next part studies the code line-by-line. If you are bored by the technicalities, feel free to jump to the animation at the end.Update: The O(log n) running time complexity applies only to n ≤ machine-word size, which is typically 64-bits today. Thanks to all the people who brought this to my attention.The technique: Recursive block-swappingThe algorithm (...)
#o-log-n #reverse-n-bit-number #c-programming #software #programming