Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this boolean "(number & 1) == 0" mean?

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.

like image 413
Andrew Martin Avatar asked Feb 16 '13 00:02

Andrew Martin


People also ask

What is an example of a Boolean?

Answer: Boolean is a primitive data type that takes either “true” or “false” values. So anything that returns the value “true' or “false” can be considered as a boolean example. Checking some conditions such as “a==b” or “a<b” or “a>b” can be considered as boolean examples.

Is a Boolean 1 or 0?

Boolean values and operationsConstant true is 1 and constant false is 0. It is considered good practice, though, to write true and false in your program for boolean values rather than 1 and 0.

What does Boolean value mean?

A Boolean value represents a truth value; that is, TRUE or FALSE. A Boolean expression or predicate can result in a value of unknown, which is represented by the null value.

How do you identify a Boolean?

To check if a value is of boolean type, check if the value is equal to false or equal to true , e.g. if (variable === true || variable === false) . Boolean values can only be true and false , so if either condition is met, the value has a type of boolean. Copied!


2 Answers

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.

like image 122
Kirby Avatar answered Oct 03 '22 05:10

Kirby


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 
like image 40
Eng.Fouad Avatar answered Oct 03 '22 05:10

Eng.Fouad