> ## 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 Complement

The `~` (bitwise complement) operator is a unary operator that performs a logical negation on each individual bit of an integral data type. It evaluates the binary representation of its operand and inverts it, flipping every `0` to `1` and every `1` to `0`.

```java theme={"dark"}
~operand
```

## Mathematical Equivalence

Because Java represents signed integers using two's complement, applying the bitwise complement operator to any integer `n` mathematically yields `-(n + 1)`.

```java theme={"dark"}
int n = 12;
int result = ~n; // Evaluates to -13
```

## Bit-Level Mechanics

When applied to a 32-bit `int`, the operator transforms the entire 32-bit sequence, including the sign bit.

```java theme={"dark"}
int a = 5;
// Binary of a:  00000000 00000000 00000000 00000101

int b = ~a;
// Binary of b:  11111111 11111111 11111111 11111010
// Decimal of b: -6
```

## Unary Numeric Promotion

The `~` operator triggers Java's unary numeric promotion rules. If the operand is of type `byte`, `short`, or `char`, the Java Virtual Machine (JVM) automatically widens it to a 32-bit `int` *before* applying the bitwise inversion. Consequently, the return type of the operation is always an `int` (unless the operand is a `long`, in which case the return type remains a 64-bit `long`).

```java theme={"dark"}
byte x = 2;

// byte y = ~x; 
// Compilation Error: Incompatible types. Possible lossy conversion from int to byte.

int y = ~x;           // Correct: The evaluated result is an int.
byte z = (byte) ~x;   // Correct: Explicit downcast required to assign back to a byte.
```

In the promotion example above, the 8-bit `byte` (`00000010`) is first sign-extended to a 32-bit `int` (`00000000 00000000 00000000 00000010`), and then all 32 bits are inverted. If explicitly cast back to a `byte`, the upper 24 bits are truncated.

<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>
