Binary AND Assign Operator

In Java, Compounded Assignment Operator combines an operation and assignment into one statement, making code concise. Below is an example on how to do it with Binary OR operation. Which is we show examples on how to use Binary AND Assign Operator in Java.

Binary AND Assign With Literal

Binary AND performs bitwise AND operations between two numbers. The operator denoted by &= will perform binary AND assign operation. Which is perform the BINARY AND, and then assign the result to the variable. Below is an example:
int a = 22;
a &= 17;
System.out.println(a);
So the above code performs bitwise AND to numbers 22 and 17, assigning the result to variable a, which gives below answer:
16

Binary AND Assign With Another variable

Below is a simple example of performing Binary AND Assign With Another variable in Java language:

int a = 57;
int b = 42;
a &= b;
System.out.println(a);

So we perform bitwise AND operation on 57 and 42, and assign the answer to variable a, which is:
40

Binary AND Assign With An Expression

We can always use any expression on the right side of the operator, for example:
int a = 125;
int b = 3;
a &= b * 6;
System.out.println(a);
So we evaluate b * 6 first, that gives the value 18. Then we perform AND operation on 125 and 18, assigning the result back to a, resulting to the value:
16