Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Bitwise NOT (~)

The unary ~ operator flips the bits of a numeric value. For Number it operates on a signed 32-bit integer; for BigInt it does not restrict the value to 32 bits. It must not be confused with logical NOT (!).

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

~expression

The operator ~expression flips every bit: 0 becomes 1, and 1 becomes 0. It first performs ToNumeric conversion, then selects the Number or BigInt operation. The result retains the selected numeric type. In contrast, ! performs logical negation and returns a boolean value.

For Number, the operand undergoes ToInt32 conversion: the fractional part is discarded toward zero, and the value wraps into the signed 32-bit integer range. NaN, both infinities, and both zeros become 0. All 32 bits are then inverted. Consequently, ~5 produces -6, ~5.9 also produces -6, and ~4294967296 produces -1. Here, the identity ~x = -x - 1 applies to the value after ToInt32 conversion, not to an arbitrary original Number value.

For BigInt, there is no 32-bit truncation. The operation corresponds to two's-complement representation with unlimited sign extension and satisfies ~x === -x - 1n. For example, ~5n produces -6n.

Conversion allows expressions such as ~"5", which produces -6, but a Symbol operand causes a TypeError. The idiom ~~x is not a general rounding method: for Number it returns ToInt32(x), so large values can wrap; for BigInt it returns the original value. The example demonstrates these differences without depending on the DOM or other Web APIs.

Examples

const value = 5;
console.log(~value);
console.log(~5n === -6n);
console.log(~5.9);
console.log(~4294967296);
console.log(~~5.9);

Result

-6
true
-6
-1
5

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources