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

# Go Bitwise NOT

The `~` (tilde) operator in Go is an approximation symbol used exclusively within generic type constraints. Introduced in Go 1.18, the expression `~T` specifies that a type parameter accepts any type whose **underlying type** is exactly `T`.

Without the `~` operator, a type constraint requires a strict type match. With the `~` operator, the compiler evaluates the underlying type of the provided type argument against `T` to determine if the constraint is satisfied.

## Syntax

The `~` operator is placed directly before a valid type within an interface or a type parameter list:

```go theme={"dark"}
// Inline constraint using ~
func Process[T ~int](val T) {
    // Implementation details
}

// Interface constraint using ~
type Number interface {
    ~int | ~int32 | ~int64 | ~float64
}
```

## Mechanics of Underlying Types

In Go, you can declare defined types based on existing types. The `~` operator dictates how these defined types are evaluated against constraints.

```go theme={"dark"}
type UserID int
type SessionID int
```

* **Strict Constraint (`int`)**: Only accepts the exact type `int`. Passing `UserID` or `SessionID` results in a compiler error.
* **Approximation Constraint (`~int`)**: Accepts `int`, `UserID`, and `SessionID` because the underlying type for all three is `int`.

## Rules and Restrictions

1. **Underlying Type Must Be Itself**: The type `T` in `~T` must be a type whose underlying type is itself. This restricts `T` to predeclared types (such as `int`, `string`, `float64`) or type literals (such as `[]string`, `map[int]bool`). You cannot use a defined type that resolves to another underlying type. For example, if `type MyInt int`, using `~MyInt` results in a compiler error: `invalid use of ~ (underlying type of MyInt is int)`.
2. **No Interfaces**: The type `T` cannot be an interface type. Applying the tilde to an interface (e.g., `~error`) is invalid.
3. **No Type Parameters**: You cannot apply the tilde to a type parameter (e.g., `type Constraint[T any, U ~T]` is invalid).
4. **Constraint Scope**: The `~` operator is strictly a compile-time construct for type constraints. It cannot be used in variable declarations, type assertions, or runtime reflection.

## Disambiguation: Bitwise NOT

Developers transitioning from C, C++, Java, or C# often expect `~` to function as the bitwise NOT (complement) operator. **Go does not use `~` for bitwise operations.**

To perform a bitwise NOT in Go, use the caret (`^`) as a unary operator:

```go theme={"dark"}
func BitwiseNotExample() {
    var y int = 42
    var x int = ^y // Equivalent to ~y in C/C++/Java
}
```

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