1

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?

3

2 Answers 2

14

This is probably the best way:

((value & -value) == value) 
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.