January 6, 2026
Rotate-gate erupts
Swapping two blocks of memory inside a larger block, in constant memory
Memory shuffle sparks a nerd brawl: Reverse vs Rotate, plus an XOR cameo
TLDR: Raymond Chen shows how to swap far-apart parts of a memory block without extra space using smart reversals, cutting work significantly. Comments ignite over two rotates vs three, cache headaches, and a cheeky XOR suggestion—mixing nostalgia and skepticism to debate what’s fastest and what’s actually practical.
Tech legend Raymond Chen dropped a brainy party trick: how to swap distant chunks inside a bigger memory blob without using extra space—think rearranging books on a jam-packed shelf with no table. He shows a clever “flip, flip, flip again” move that cuts the work in half, and the crowd instantly split into camps. Team Reverse cheered the simplicity and minimal moves, while Team Rotate flexed with “you can do it in two spins,” backed by a link in the replies. Old-school fans chimed in with “Programming Pearls did it first,” and tossed in cache drama: the “cycle” method can thrash your computer’s memory habits. That’s when the XOR trick crashed the party, drawing collective side-eye and meme replies (“XOR? In this economy?”). One commenter called the whole thing “unsatisfying,” like a puzzle that’s clever but not practical, while another praised Chen’s no-fluff approach. Bonus chatter: big-name libraries secretly use fancy cycle moves for random-access cases (fast jumping), with special cases for tiny spins. The comment section? A mix of nostalgia, performance nerd fights, and jokes—aka peak internet. Reverse, rotate, or XOR? The only constant is the drama.
Key Points
- •Swapping non-adjacent blocks in a contiguous memory region can be done in-place without allocating extra memory.
- •Three std::rotate operations can achieve the swap but cost about 2n swaps, where n is the total size of the affected segments.
- •Rotate can be implemented by reversing subranges and then reversing the whole range, making rotate of n elements cost n swaps.
- •An optimized method reverses the two target blocks and the middle block, then reverses the combined range, reducing cost to n swaps.
- •std::rotate requires forward iterators, std::reverse requires bidirectional iterators; libraries optimize rotate for random-access iterators using cycle decomposition to n/2 swaps.