The bitwise NOT operator (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.
~) is a unary operator that inverts the bits of its numeric operand. Its exact execution mechanics and return type depend on whether the operand evaluates to a standard JavaScript Number or a BigInt.
Execution Mechanics: Standard Number
When the ~ operator is applied to a standard Number (or a non-BigInt value that coerces to a Number), the JavaScript engine performs the following sequence of operations:
- Type Coercion: The operand is coerced into a numeric value.
- Integer Conversion (
ToInt32): The numeric value is converted to a 32-bit signed integer using Two’s complement representation. Fractional components are truncated. Special numeric values (NaN,Infinity,-Infinity) are converted to0. - Bitwise Inversion: Every bit in the 32-bit sequence is flipped, transforming every
0to1and every1to0. - Return: The resulting 32-bit integer is returned as a standard JavaScript
Number(IEEE 754 double-precision float).
Execution Mechanics: BigInt
When the ~ operator is applied to a BigInt primitive, the behavior changes to accommodate arbitrary-precision integers:
- No Truncation: The JavaScript engine does not perform a
ToInt32conversion. The value is not truncated to 32 bits. - Infinite Two’s Complement Inversion: The operator flips the bits of the
BigInt. BecauseBigIntvalues do not have a fixed bit-width, the operation conceptually treats the value as a Two’s complement representation with an infinite number of leading sign bits. - Return: The result is returned as a
BigInt.
Mathematical Equivalence
Because JavaScript represents signed integers using Two’s complement, the bitwise inversion of any integerx (whether a Number or a BigInt) mathematically evaluates to -(x + 1).
Evaluation Examples
Standard Integers (Number):
5in 32-bit binary:00000000000000000000000000000101- Inverted bits:
11111111111111111111111111111010 - Two’s complement decimal value:
-6
BigInt):
ToInt32 conversion on standard numbers, fractional parts are discarded before inversion.
BigInt) are coerced to standard numbers before the ToInt32 conversion.
Master JavaScript with Deep Grasping Methodology!Learn More





