Java
&= operator
bitwise AND
logical AND
Java operators

Does the Java operator apply or ?

Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms

Introduction

The Java operator &= is a compound assignment form of bitwise AND. It often gets confused with &&, but the two operators solve different problems: &= updates a variable using a bitwise AND, while && evaluates boolean conditions with short-circuit logic.

Understanding that difference is important because the operators may both involve the idea of "AND" while behaving very differently in code.

What &= Means

The statement:

java
a &= b;

is equivalent to:

java
a = a & b;

For integer types, & performs a bitwise AND. Each bit of a is compared with the corresponding bit of b, and the result bit is 1 only if both input bits are 1.

Example:

java
1public class Demo {
2    public static void main(String[] args) {
3        int a = 12; // 1100
4        int b = 10; // 1010
5
6        a &= b;     // 1000
7        System.out.println(a); // 8
8    }
9}

This is useful when you are applying a mask to keep only certain bits.

Bitwise And Versus Logical And

The && operator is not the same thing. It is a logical AND operator for boolean expressions:

java
1public class Demo {
2    public static void main(String[] args) {
3        boolean result = (5 > 3) && (10 > 7);
4        System.out.println(result);
5    }
6}

This evaluates to true because both conditions are true.

A key feature of && is short-circuiting. If the left side is false, Java does not evaluate the right side because the whole expression must already be false.

Why Short-Circuiting Matters

Short-circuiting prevents unnecessary work and can avoid errors:

java
1public class Demo {
2    public static void main(String[] args) {
3        int x = 0;
4        boolean safe = (x != 0) && (10 / x > 1);
5        System.out.println(safe);
6    }
7}

Because (x != 0) is false, Java never evaluates (10 / x > 1), so there is no division-by-zero exception.

That behavior does not belong to &=. The compound operator is about assignment after bitwise combination, not conditional evaluation flow.

& Can Also Work On Booleans

Java also allows & on boolean values, but unlike &&, it does not short-circuit:

java
1public class Demo {
2    public static void main(String[] args) {
3        boolean result = (5 > 8) & (10 > 1);
4        System.out.println(result);
5    }
6}

This still evaluates both sides. That makes & and && another important distinction to keep in mind when reading or writing Java code.

When &= Is Useful

The &= operator is most common in low-level or flag-oriented code. For example, you may want to clear or preserve selected bits in a status value.

That is a very different use case from the one handled by &&, which is usually found in if, while, and guard conditions.

Common Pitfalls

The biggest mistake is confusing &= with &&. One is compound bitwise assignment and the other is logical short-circuit AND.

Another pitfall is forgetting that & on booleans does not short-circuit. If the right side is expensive or unsafe, && is often the intended operator.

A third issue is using bitwise operators without understanding the underlying binary representation. In flag code, it helps to write comments or use named masks so the operation stays readable.

Summary

  • 'a &= b means a = a & b.'
  • For integers, & performs a bitwise AND.
  • '&& is a logical AND operator with short-circuit evaluation.'
  • '& can also be used on booleans, but it evaluates both sides.'
  • Use &= for bit masking and && for conditional logic.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms