问题
Possible Duplicate:
How to check if a number is a power of 2
How could I write a method that would return true if passed in the value 2, 4, 8, 32, 64, and so on?
回答1:
This is probably the best way:
((value & -value) == value)
回答2:
Might want to look at this if you need a fast algorithm:
http://en.wikipedia.org/wiki/Power_of_two#Fast_algorithm_to_check_if_a_positive_number_is_a_power_of_two
来源:https://stackoverflow.com/questions/9146855/determine-if-num-is-a-power-of-two-in-java