# 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.
###### Who is Participating?

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.

Senior Software DeveloperCommented:
``````((x != 0) && !(x & (x - 1)));
``````

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

``````((x != 0) && ((x & (x - 1)) == 0);
``````
Author 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?
Senior Software DeveloperCommented:

http://stackoverflow.com/questions/600293/how-to-check-if-a-number-is-a-power-of-2
Sr. 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");
}
}
}
Author Commented:
can you please explain what this means

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

thanks all.
Senior Java ArchitectCommented:
If your number is greater than zero and Integer.bitCount(yourNumber) returns 1, your number is a power of two.
Senior 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

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

Senior 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.
Sr. ConsultantCommented:
Perfect.  :)
Author Commented:
thanks
>>100000 & 011111 = 000000 so 32 is a power of 2.

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