Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
可能重复: 如何检查一个数字是否是 2 的幂
如果传入值 2、4、8、32、64 等,我如何编写一个返回 true 的方法?
这可能是最好的方法:
((value & -value) == value)
如果您需要快速算法,可能想看看这个:
http://en.wikipedia.org/wiki/Power_of_two#Fast_algorithm_to_check_if_a_positive_number_is_a_power_of_two