~ operator is the unary bitwise complement operator. It performs a logical NOT operation on the binary representation of a number, inverting every bit so that each 0 becomes 1 and each 1 becomes 0.
Syntax
Type Support
The operator is defined for the following types in the Dart core library:int: Performs bitwise inversion on the platform-specific integer representation.BigInt: Performs bitwise inversion on an arbitrarily large integer.
operator ~().
Platform Semantics
The bit-width used for the operation depends on the compilation target. Dart Native (VM, Mobile, Desktop) Theint type is a 64-bit two’s complement integer. The operator inverts all 64 bits.
Dart Web (JavaScript)
The int type maps to a JavaScript number (IEEE 754 double-precision float). When the ~ operator is applied:
- The operand is truncated to a 32-bit signed integer.
- The bits are inverted within this 32-bit range.
- The result is a 32-bit signed integer.
Note: Because of this truncation, applying ~ to an integer larger than 32 bits on the web will result in data loss before the inversion occurs.
Mathematical Relationship
For integers using two’s complement representation, the relationship between the effective operand and the result is defined by the following formula: On Dart Web, this formula applies to the truncated 32-bit value, not necessarily the original operand if it exceeds 32 bits.| Operand () | Binary Representation | Complement () | Result Decimal |
|---|---|---|---|
0 | ...0000 | ...1111 | -1 |
1 | ...0001 | ...1110 | -2 |
-1 | ...1111 | ...0000 | 0 |
Binary Visualization
The following example demonstrates the inversion of the integer5 (binary ...0000 0101).
Master Dart with Deep Grasping Methodology!Learn More





