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

# Kotlin Bitwise NOT

The `inv()` function in Kotlin performs a bitwise inversion (also known as a bitwise NOT operation) on integer data types. It systematically flips every bit in the binary representation of the operand, converting all `0` bits to `1` and all `1` bits to `0`.

Unlike languages such as Java or C++ that utilize the tilde (`~`) symbol for bitwise NOT operations, Kotlin implements bitwise operations as named functions. For signed integers, the `inv()` function is exclusively available on `Int` and `Long`.

Because Kotlin represents signed integers using two's complement, applying the `inv()` function to a value `x` mathematically equates to `-(x + 1)`. The sign bit is flipped along with the magnitude bits, which inherently changes the sign of the integer.

```kotlin theme={"dark"}
val a: Int = 43
// Binary representation (32-bit): 
// 00000000 00000000 00000000 00101011

val b: Int = a.inv()
// Binary representation after inv() (32-bit): 
// 11111111 11111111 11111111 11010100
// Decimal equivalent: -44

val c: Long = 1L
val d: Long = c.inv()
// Binary representation after inv() (64-bit):
// 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111110
// Decimal equivalent: -2
```

## Handling Smaller Signed Types

The `inv()` function is not a member of the signed `Byte` or `Short` classes. To perform a bitwise inversion on these smaller integral types, the value must be explicitly widened to an `Int`, inverted, and then narrowed back to the original type.

```kotlin theme={"dark"}
val byteValue: Byte = 0b00001111 // Decimal: 15

// val errorByte = byteValue.inv() // COMPILATION ERROR: Unresolved reference: inv

val invertedByte: Byte = byteValue.toInt().inv().toByte() 
// Binary: 11110000
// Decimal equivalent: -16
```

## Unsigned Types

Kotlin also supports `inv()` on all unsigned integer types (`UByte`, `UShort`, `UInt`, `ULong`). Unlike their signed counterparts, smaller unsigned types like `UByte` and `UShort` possess their own bitwise member functions and preserve their data types without requiring manual widening.

The bit-flipping mechanism remains identical, but because unsigned types do not use a sign bit or two's complement for negative values, the resulting decimal interpretation differs significantly from signed types.

```kotlin theme={"dark"}
val uByteVal: UByte = 15u
val invertedUByte: UByte = uByteVal.inv() // Compiles directly, type is preserved
// Binary: 11110000
// Decimal equivalent: 240

val uIntVal: UInt = 43u
// Binary: 00000000 00000000 00000000 00101011

val invertedUInt: UInt = uIntVal.inv()
// Binary: 11111111 11111111 11111111 11010100
// Decimal equivalent: 4294967252
```

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