> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Java Bitwise AND Assignment

The `&=` operator is a compound assignment operator that performs a bitwise AND (for integral types) or a logical AND (for boolean types) operation between the left and right operands, assigning the computed result back to the left operand.

## Syntax and Evaluation

```java theme={"dark"}
variable &= expression;
```

Internally, the Java compiler evaluates `a &= b` as `a = (T) (a & b)`, where `T` is the declared data type of `a`. This includes an implicit narrowing primitive conversion, which prevents compilation errors when operating on types smaller than `int` (such as `byte`, `short`, or `char`) that are automatically promoted to `int` during bitwise operations.

Crucially, Java semantics dictate that the left-hand operand in a compound assignment is evaluated **exactly once**. This distinguishes `a &= b` from its expanded form `a = (T) (a & b)` when the left operand contains side effects. For example, the expression `arr[i++] &= b` evaluates the array access and increments `i` only once, whereas `arr[i++] = (T) (arr[i++] & b)` would evaluate the array access and increment `i` twice.

## Behavior by Data Type

**1. Integral Types (`byte`, `short`, `int`, `long`, `char`)**
When applied to integral types, `&=` performs a bit-by-bit comparison of the binary representations of both operands. A bit in the resulting value is set to `1` if and only if the corresponding bits in both operands are `1`. Otherwise, the resulting bit is `0`.

```java theme={"dark"}
int a = 12;  // Binary representation: 0000 1100
int b = 10;  // Binary representation: 0000 1010

a &= b;      // Bitwise AND operation
             // 0000 1100
             // 0000 1010
             // ------
             // 0000 1000 (Decimal: 8)
             
// a is now 8
```

**2. Boolean Type (`boolean`)**
When applied to boolean variables, `&=` performs a strict logical AND operation. The left operand is assigned `true` if and only if both the initial value of the left operand and the evaluated right operand are `true`. Otherwise, it is assigned `false`.

Unlike the conditional AND operator (`&&`), the `&=` operator **does not short-circuit**. The right-hand expression is always evaluated, regardless of the initial state of the left-hand operand.

```java theme={"dark"}
boolean flag = false;

// The method evaluateCondition() WILL be executed even though flag is already false.
flag &= evaluateCondition(); 
```

## Implicit Casting Mechanics

Because compound assignment operators automatically cast the result back to the left operand's type, `&=` bypasses the explicit casting requirement that a standard `&` operation would demand when working with narrower primitive types.

```java theme={"dark"}
byte b1 = 12;
byte b2 = 10;

// Standard bitwise AND requires explicit casting because operands are promoted to int
// b1 = b1 & b2; // Compilation error: incompatible types: possible lossy conversion from int to byte
b1 = (byte) (b1 & b2); // Compiles

// Compound assignment handles the cast implicitly
b1 &= b2; // Compiles successfully
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Java Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
