News for hackers. Stuff that matters.
Re:Low Level Bit Hacks You Absolutely Must Know by yters on Tuesday June 30, 2009 @04:51PM (#681553)
Why would I want to swap the same var with itself?
Re:Low Level Bit Hacks You Absolutely Must Know by ori_b on Tuesday June 30, 2009 @09:10PM (#681922)
Assume you had a sorting algorithm that swaps values, and in some cases would swap two of the same values. It's easier (and often faster, thanks to the high cost of branch misprediction) to simply assume the swap of something with itself is a no-op than to explicitly check the address of the value to make sure it isn't the same. Or you could have just forgotten to make the check, and allowed the user to pass in their own swap functions (say, to deep-copy structs in C).
Re:Low Level Bit Hacks You Absolutely Must Know by sofal on Tuesday June 30, 2009 @05:54PM (#681665)
Your code should be robust enough to provide an intuitive default behavior for corner cases like that. That way the application code doesn't need to be cluttered with special conditions.
Re:Low Level Bit Hacks You Absolutely Must Know by scott_s on Tuesday June 30, 2009 @05:16PM (#681599)
You wouldn't, but let's say you wrote a function with the signature

  fast_swap_values(unsigned long*, unsigned long*)
Users of the function could accidentally pass in the same addresses.