Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the &= operator for in Java

Tags:

java

The Java tutorials here mention that &= is an assignment operator but doesn't seem to mention what it does.

What does &= do?

like image 301
dann.dev Avatar asked Mar 27 '12 02:03

dann.dev


1 Answers

a &= x

is equivalent to

a = (type of a)(a & x)

which in turn is a

  • bitwise AND of a and x in the case where a and x are integers or a
  • non short-circuiting logical AND in the case of a and x being booleans (which means that x will be evaluated in any case here, even if a is false).

There are several other binary operators which can be used with similar semantics, like +=, -=, *=, /=, %=, |=, <<=, ...

like image 168
Niklas B. Avatar answered Sep 21 '22 04:09

Niklas B.