Get the latest tech news

Why is x & -x equal to the largest power of 2 that divides x?


Or how to find the largest power of 2 that divides x?

I agree, this is a terrible explanation, but if you contrast with how we represent in base 10, that is, decimal, then it should make sense. Coming back to our original concern, x & -x gives you the largest power of 2 that divides x only when-x is represented in two’s complement. We have a nice mathemagical property involving the largest power of b in base b format.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of x & -x

x & -x

Photo of largest power

largest power