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

The author delves into the mathematical concept of x & -x equaling the largest power of 2 that divides x. The explanation begins with how numbers are represented in binary, leading to discussions on one’s complement and two’s complement for negative numbers. The focus then shifts to the mathemagical property involving the largest power of 2 in binary format, uncovering the reasoning behind the result of x & -x. The conclusion highlights the importance of understanding two’s complement in grasping the logic behind this operation. The author admits to initially struggling with the concept but eventually comprehended it through logical reasoning.


To top