~ Operator (C# Reference)

The bitwise complement operator ~ is a unary operator that produces a bitwise complement of its operand by reversing each bit. All integer types support the ~ operator.

Note

The ~ symbol is also used to declare finalizers. For more information, see Finalizers.

The following example demonstrates the usage of the ~ operator:

uint a = 0b_0000_1111_0000_1111_0000_1111_0000_1100;
uint b = ~a;
Console.WriteLine(Convert.ToString(b, toBase: 2));
// Output:
// 11110000111100001111000011110011

Note

The preceding example uses the binary literals introduced in C# 7.0 and enhanced in C# 7.2.

Operator overloadability

User-defined types can overload the ~ operator.

C# language specification

For more information, see the Bitwise complement operator section of the C# language specification.

See also