Reversing an n-bit number in O(log n)

In each iteration of the mask update, all blocks are shifted left by half their size..When a block is XORed with the previous mask, half of the block overlaps with zeros and the other half overlaps with ones..This creates 2 blocks in the new mask, each half the size of the previous blocks..Here is an illustration:0000000011111111 // original mask, 8-bit blocks0000111111110000 // mask shifted left by block-size/20000111100001111 // XORed: new mask, 4-bit blocksTying it all togetherHere’s a short animation that shows the algorithm in action:It’s amazing how much depth can be found in 9 lines of code..If you have in mind an interesting piece of code that you’d like me to explore, please leave a response below!. More details

Leave a Reply