That XOR Trick (2020)
florian.github.io
That XOR Trick (2020)
1–10 of 141 posts
Re: That XOR Trick (2020)
#2Wow! This is a flashback! Hope you're doing well Andy W!
Re: That XOR Trick (2020)
#3Another fun trick I've discovered.
`XOR[0...n] = 0 ^ 1 .... ^ n = [n, 1, n + 1, 0][n % 4]`
Re: That XOR Trick (2020)
#4The first thing that occurred to me is that if a number is missing from a list, the sum of that list will fall short. But I like XOR's.
Re: That XOR Trick (2020)
#5Why do people hate traditional for loops so much? In a conversation about petty micro optimizations, we end up performing two loops instead of one, all because sticking three operations in one statement is "yucky"?
Re: That XOR Trick (2020)
#6Why do people hate traditional for loops so much? In a conversation about petty micro optimizations, we end up performing two loops instead of one, all because sticking three operations in one statement is "yucky"?
I think it's just an interesting approach to solving particular limited problems. If I needed to solve this I'd end up either using set arithmetic or sorting the list, both of which use more memory and time. Maybe down low in some compiler loop or JVM loop this could be the difference between a sluggish application and a snappy one
Re: That XOR Trick (2020)
#7The first thing that occurred to me is that if a number is missing from a list, the sum of that list will fall short. But I like XOR's.
It really tickles my brain in a lovely way that it avoids all overflow risk as well
Re: That XOR Trick (2020)
#8Another fun trick I've discovered. `XOR[0...n] = 0 ^ 1 .... ^ n = [n, 1, n + 1, 0][n % 4]`
Right, or in summary, no you don't need to all that extra work up front.
Re: That XOR Trick (2020)
#9Generalizing an 'xor accumulator' support set difference of more than one element is interesting: https://github.com/bitcoin-core/minisketch
Re: That XOR Trick (2020)
#10Ah, my least favorite technical interview question. (I've been asked it, but only after I first read about it online.)