More Bit Hacks
August 20, 2013
We studied three bit hacks in a previous exercise. In today’s exercise we look at three more:
1) Compute the minimum (or maximum) of two integers without branching.
2) Determine if an integer is a power of two.
3) Swap the values of two variables without using any extra space.
Your task is to write bit hacks for the three tasks noted above; you may want to find multiple solutions for some of them. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
I suppose one could argue that 0 is a power of 2 because 0 = pow(2, -∞).