What does the ampersand do?
Right, I recently came across with this short piece of code:
Outputs: "bar, foo, bar, foo"
for(int a=0; a<3; a++)
The thing I do not understand in this code is the line " if( (a&1)==1 )". Doesn't & stand for "AND" in Java? IF a == 1 AND 1 == 1 DO... But that in that case the code would simply output "bar, foo, bar, bar". Could someone please explain this line to me?
Re: What does the ampersand do?
The & is the bitwise AND operator. See:
Bitwise and Bit Shift Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
The expression: (a&1)==1 tests if the low order bit of the variable a is 1. That would be true if a is an odd number.