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

# PHP Bitwise NOT

The `~` (tilde) operator in PHP is the bitwise NOT operator. It is a unary operator that performs logical negation on each individual bit of its operand, flipping every `0` to `1` and every `1` to `0` at the binary level.

```php theme={"dark"}
$result = ~$operand;
```

## Behavior with Integers

When applied to an integer, the `~` operator evaluates the value using the system's native signed integer representation (typically 64-bit Two's Complement). Because the sign bit is also inverted, applying the bitwise NOT operator to an integer `x` mathematically yields `-(x + 1)`.

```php theme={"dark"}
$x = 5; 
// 64-bit binary: 00000000...00000101

$y = ~$x; 
// 64-bit binary: 11111111...11111010 (Two's complement for -6)

var_dump($y); // int(-6)
```

## Behavior with Strings

Unlike many strictly typed languages, PHP allows bitwise operations directly on strings. When the `~` operator is applied to a string, it iterates through the string and performs a bitwise NOT operation on the underlying byte (ASCII) value of every single character. The length of the resulting string is identical to the length of the original string.

```php theme={"dark"}
$char = 'a'; 
// ASCII 97, Binary: 01100001

$inverted = ~$char; 
// Binary: 10011110, ASCII 158 (Extended ASCII)

var_dump(bin2hex($inverted)); // string(2) "9e"
```

## Implicit Type Casting and Type Errors

If the operand is a float, a boolean, or the special `null` type, PHP implicitly casts the value to an integer before applying the bitwise inversion. However, applying the `~` operator to non-scalar types like arrays or objects is invalid and will throw a `TypeError`.

```php theme={"dark"}
// Floats are truncated to integers before inversion.
// Note: Since PHP 8.1, implicitly converting a float with a fractional 
// part to an integer emits a deprecation warning.
$float = 2.9; 
var_dump(~$float); 
// Deprecated: Implicit conversion from float 2.9 to int loses precision
// int(-3)

// Booleans are cast to 1 or 0
$boolTrue = true; // Cast to int(1)
var_dump(~$boolTrue); // int(-2)

$boolFalse = false; // Cast to int(0)
var_dump(~$boolFalse); // int(-1)

// Null is cast to 0
$nullVal = null; // Cast to int(0)
var_dump(~$nullVal); // int(-1)

// Arrays and Objects throw a TypeError
$arr = [];
var_dump(~$arr); 
// Fatal error: Uncaught TypeError: Cannot perform bitwise not on array
```

## Operator Precedence

The `~` operator has very high precedence in PHP, evaluating before most arithmetic operators (`+`, `-`, `*`, `/`), bitwise shift operators (`<<`, `>>`), and logical operators. However, the exponentiation operator (`**`) has **higher** precedence than the unary `~` operator.

```php theme={"dark"}
$val1 = ~2 + 3; 
// Evaluates as (~2) + 3 -> (-3) + 3 -> 0
// It does NOT evaluate as ~(2 + 3)

$val2 = ~2 ** 3;
// Evaluates as ~(2 ** 3) -> ~(8) -> -9
// It does NOT evaluate as (~2) ** 3 -> (-3) ** 3 -> -27
```

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