JavaScript stores numbers as 64-bit floating point values, but all bitwise operations are conducted on 32-bit binary numbers.
Before performing a bitwise operation, JavaScript converts numbers to 32-bit signed integers.
After the operation, the result is converted back to 64-bit JavaScript numbers.
The examples above use 4-bit unsigned binary numbers, which explains why ~5 returns 10. However, since JavaScript employs 32-bit signed integers, it does not return 10; it returns -6. In binary, this is represented as: 00000000000000000000000000000101 (5) 11111111111111111111111111111010 (~5 = -6) In signed integers, the leftmost bit indicates the sign (negative). |