'Given an integer how can I determine the highest alignment of its value?

My solution would have been to set ones as the last bits and & them with the value to test, and if it's zero then it's aligned for the number of bits set (times 2, or bitshifted 1). For example if the two least significant bits are set and I & with the value, and get 0, then it's aligned for 2 * 2 = 4. Is this right? Is this the best way to do it?

Or is there a library function that does this? I'm using c++.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source