What's the purpose of & in Java?

131 views Asked by At

So, I was following a pacman tutorial and the coder uses & not &&. He doesn't explain what it does though. I searched it up and it says that unlike &&, & checks both statements. That makes sense. But the coder doesn't use it in a comparison context. This is his code.

screenData[] is an 255 element int array.

int ch = 0, pos = 0;
if((ch & 16) != 0) {
      screenData[pos] = (int) (ch & 15);
    }

Please tell me why he did that and what's the use of & in this context.

3

There are 3 answers

1
berry.11 On BEST ANSWER

The & operator used in the code is the bitwise AND operator. This operator checks the corresponding bits of the two operands at the bit level and generates its result.

In the code snippet, the second bit (corresponding to 16) of the variable ch is checked using the ch & 16 expression. If the second bit is 1 (not 0), the last 4 bits of ch (the corresponding bits 15) are assigned to the screenData[pos] array. This usage is used to control certain bits of ch and act accordingly.

For example, the expression ch & 16 is used to check the second bit of the variable ch, while the expression ch & 15 is used to get the last 4 bits of ch. Hence, the & operator is used to control or manipulate certain bits in bitwise operations.

0
Konrad Rudolph On

& performs bitwise “and”.

Thus, ch & 15 checks whether any of the first four lowest bits are set (because 15 is 1111 in binary). The result of the operation is a number with all other bits set to 0, and only those of the four low-level bits set to 1 which were also set to 1 in ch.

3
WJS On

What's the purpose of & in Java?

There are two uses for the & (AND) operator

  • the most common is as a bit masking operation in combination with other bit manipulation operators (e.g ~, |, ^) to check for certain bits or to mask off certain bits.
  • the other is for logical comparisons.
    • && for short circuiting conditionals
    • & for non-short circuiting conditionals

Bit Masking, etc

int v = 0xFF;
v = v & 0xF0; //now v = 0xF0

In Conditionals

int v = 10;
int k = 1;
if (v == 11 && k++ == 4) {
   // do something
}
System.out.println(k); // prints 1

since v is false, there was no reason to check if k++ == 4 so k is not incremented. In other words it efficiently short circuits the evaluation. This is because, when a series of && separates various logical tests, if one fails, the whole expression is evaluated as false. Unlike the || (OR operator), if a all the expressions are OR's and evaluate false except for one, the expression is true.

int v = 10;
int k = 1;
if (v == 11 & k++ == 4) {
   // do something
}
System.out.println(k); // prints 2

With a single & k is tested regardless of the outcome of the first or even the second part of the conditional. This is, imho, less used and can induce bugs that are hard to find.

In your example, here is what is going on.

int ch = 0, pos = 0;
if((ch & 16) != 0) { //16 is 10000 in binary so it checks to see if that 
                     //bit is set, if `not ==  0`, it is set, so the 
                     //conditional succeeds
                
      screenData[pos] = (int) (ch & 15); // 15 is 1111 in binary to it 
                                         // masks off the high order bits
                                         // keeping the low   
                                         // order four bits of ch and 
                                         // assigns the result to the array.

The above example would be more interesting if ch wasn't 0 to start.