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 means an even number but 6 is not a power of 2.

Any ideas?
Who is Participating?
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 SuterCommented:
((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 SuterCommented:
This article explains in more detail what's going on.
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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 SuterCommented:
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:


However, 9 would look like this:


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 SuterCommented:
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:
>>100000 & 011111 = 000000 so 32 is a power of 2.

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

This article explains all the bitwise operators and how they work.
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

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.