java : power of 2

Dear experts
I want to know how we can check if a number is power of 2.
For example
2     2 power 1
4     2 power 2
8     2 power 3
16   2 power 4
..and so on

If number % 2 == 0 ..it means an even number but 6 is not a power of 2.

Any ideas?
thanks.
royjaydAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russ SuterSenior Software DeveloperCommented:
((x != 0) && !(x & (x - 1)));

Open in new window


Or for Java you may have to write it this way:

((x != 0) && ((x & (x - 1)) == 0);

Open in new window

royjaydAuthor Commented:
not sure what you mean
((x != 0) && ((x & (x - 1)) == 0);

So for 6 which is not a power of 2 i have

((6 != 0) && ((6 & (6 - 1)) == 0);   >> what does this (in bold) do?
Russ SuterSenior Software DeveloperCommented:
This article explains in more detail what's going on.

http://stackoverflow.com/questions/600293/how-to-check-if-a-number-is-a-power-of-2
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Amitkumar PSr. ConsultantCommented:
Hi royjayd,

Russ Suter is perfectly correct. You will need to use bitwise AND operator to figure out if number is power of 2.

Give it a try with below method. It will explain rest of the thing.

      public static void main(String[] args) {
            for (int i=0; i<100; i++) {
                  
                  System.out.print("I : "+i+", I & (I-1) : "+(i&(i-1))+", ");
                  
                  if (((i != 0) && ((i & (i - 1)) == 0))) {
                        System.out.println(" Power of 2");
                  } else {
                        System.out.println(" Not power of 2");
                  }
            }
      }
royjaydAuthor Commented:
can you please explain what this means

<< if (((i != 0) && ((i & (i - 1)) == 0))) >>

thanks all.
CPColinSenior Java ArchitectCommented:
If your number is greater than zero and Integer.bitCount(yourNumber) returns 1, your number is a power of two.
Russ SuterSenior Software DeveloperCommented:
the first part checks to make sure your number is not zero since zero is technically not a power of 2.

The second part takes your number and performs a bitwise and operation on your number minus one. The result is checked to make sure it's a power of 2. Ignoring the first part (because the sample numbers below will not be zero) here's what's going on.

Take the number 8;

8 & (8 - 1) == 0 is true. In binary it looks like this:

1000
0111
-------
0000

However, 9 would look like this:

1001
0111
-------
0001

Which is not zero and therefore the number is not a power of 2.

Binary math is awesome!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Russ SuterSenior Software DeveloperCommented:
Allow me to try and explain in more detail.

Any number that is a power of 2 will be represented in binary as a 1 followed by 1 or more zeros. Example:

32 = 100000 in binary

Subtracting 1 from 32 = 31:

31 = 011111 in binary

100000 & 011111 = 000000 so 32 is a power of 2.
Amitkumar PSr. ConsultantCommented:
Perfect.  :)
royjaydAuthor Commented:
thanks
>>100000 & 011111 = 000000 so 32 is a power of 2.

in binary what does &  mean , is it like a add or subtract ?
Russ SuterSenior Software DeveloperCommented:
& means perform a bitwise AND operation. It's a form of binary addition.

This article explains all the bitwise operators and how they work.

http://code.tutsplus.com/articles/understanding-bitwise-operators--active-11301
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.