A number in the form 2 ^ n is written in binary form by 1 with a sequence of 0 or more than 0. For example, 1, 10, 100, 1000, ... etc. All have a power of 2.
To get a maximum power of 2 dividing a given number, you can follow these two steps:
Enter the number in binary form. For example, if the number is 168, write 10101000.
Delete all the bits before the first bit on the right side that contains 1. In case of 168, the remaining 1000 (= 8 in decimal) after deleting the first part of 10101000.
Your result remains - that is, the maximum power of 2 dividing the number.
Programmatically, let x be your input number. Then your desired result would be: x - (x ^ (x-1))
Explanation:
x ^ (x-1) [meaning x XOR x-1] gets rid of the first LSB side (LSB)
x - (x ^ (x-1)) gets rid of the remaining part and saves only the first 1 from the LSB side.
Kunal das
source share