What does this boolean "(number & 1) == 0" mean?
JavaMethodsArraylistBooleanIntJava Problem Overview
On CodeReview I posted a working piece of code and asked for tips to improve it. One I got was to use a boolean method to check if an ArrayList had an even number of indices (which was required). This was the code that was suggested:
private static boolean isEven(int number)
{
return (number & 1) == 0;
}
As I've already pestered that particular user for a lot of help, I've decided it's time I pestered the SO community! I don't really understand how this works. The method is called and takes the size of the ArrayList as a parameter (i.e. ArrayList has ten elements, number = 10).
I know a single &
runs the comparison of both number and 1, but I got lost after that.
The way I read it, it is saying return true if number == 0
and 1 == 0
. I know the first isn't true and the latter obviously doesn't make sense. Could anybody help me out?
Edit: I should probably add that the code does work, in case anyone is wondering.
Java Solutions
Solution 1 - Java
Keep in mind that "&" is a bitwise operation. You are probably aware of this, but it's not totally clear to me based on the way you posed the question.
That being said, the theoretical idea is that you have some int, which can be expressed in bits by some series of 1s and 0s. For example:
...10110110
In binary, because it is base 2, whenever the bitwise version of the number ends in 0, it is even, and when it ends in 1 it is odd.
Therefore, doing a bitwise & with 1 for the above is:
...10110110 & ...00000001
Of course, this is 0, so you can say that the original input was even.
Alternatively, consider an odd number. For example, add 1 to what we had above. Then
...10110111 & ...00000001
Is equal to 1, and is therefore, not equal to zero. Voila.
Solution 2 - Java
You can determine the number either is even or odd by the last bit in its binary representation:
1 -> 00000000000000000000000000000001 (odd)
2 -> 00000000000000000000000000000010 (even)
3 -> 00000000000000000000000000000011 (odd)
4 -> 00000000000000000000000000000100 (even)
5 -> 00000000000000000000000000000101 (odd)
6 -> 00000000000000000000000000000110 (even)
7 -> 00000000000000000000000000000111 (odd)
8 -> 00000000000000000000000000001000 (even)
&
between two integers is bitwise AND operator:
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
So, if (number & 1) == 0
is true
, this means number
is even.
Let's assume that number == 6
, then:
6 -> 00000000000000000000000000000110 (even)
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
1 -> 00000000000000000000000000000001
-------------------------------------
0 -> 00000000000000000000000000000000
and when number == 7
:
7 -> 00000000000000000000000000000111 (odd)
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
1 -> 00000000000000000000000000000001
-------------------------------------
1 -> 00000000000000000000000000000001
Solution 3 - Java
&
is the bitwise AND operator. &&
is the logical AND operator
In binary, if the digits bit is set (i.e one), the number is odd.
In binary, if the digits bit is zero , the number is even.
(number & 1)
is a bitwise AND test of the digits bit.
Another way to do this (and possibly less efficient but more understandable) is using the modulus operator %
:
private static boolean isEven(int number)
{
if (number < 0)
throw new ArgumentOutOfRangeException();
return (number % 2) == 0;
}
Solution 4 - Java
This expression means "the integer represents an even number".
Here is the reason why: the binary representation of decimal 1
is 00000000001
. All odd numbers end in a 1
in binary (this is easy to verify: suppose the number's binary representation does not end in 1
; then it's composed of non-zero powers of two, which is always an even number). When you do a binary AND
with an odd number, the result is 1
; when you do a binary AND
with an even number, the result is 0
.
This used to be the preferred method of deciding odd/even back at the time when optimizers were poor to nonexistent, and %
operators required twenty times the number of cycles taken by an &
operator. These days, if you do number % 2 == 0
, the compiler is likely to generate code that executes as quickly as (number & 1) == 0
does.
Solution 5 - Java
Single &
means bit-wise and
operator not comparison
So this code checks if the first bit
(least significant/most right) is set or not, which indicates if the number is odd
or not; because all odd numbers will end with 1
in the least significant bit e.g. xxxxxxx1
Solution 6 - Java
&
is a bitwise AND
operation.
For number = 8:
1000
0001
& ----
0000
The result is that (8 & 1) == 0
. This is the case for all even numbers, since they are multiples of 2 and the first binary digit from the right is always 0. 1 has a binary value of 1 with leading 0s, so when we AND
it with an even number we're left with 0.
Solution 7 - Java
The &
operator in Java is the bitwise-and operator. Basically, (number & 1)
performs a bitwise-and between number
and 1
. The result is either 0 or 1, depending on whether it's even or odd. Then the result is compared with 0 to determine if it's even.
Here's a page describing bitwise operations.
Solution 8 - Java
It is performing a binary and against 1, which returns 0 if the least significant bit is not set
for your example
00001010 (10)
00000001 (1)
===========
00000000 (0)
Solution 9 - Java
This is Logical design concept bitwise & (AND)operater.
return ( 2 & 1 ); means- convert the value to bitwise numbers and comapre the (AND) feature and returns the value.
Prefer this link http://www.roseindia.net/java/master-java/java-bitwise-and.shtml