The bitwise NOT operator
suggest changeThe bitwise NOT (~) flips each bit in a value. The operand is converted with ToInt32 first, so the operation uses 32 bits. The patterns below are shortened. The numeric result is -(x + 1) for any integer in that range.
Syntax:
~expression
Returns:
- a
Numberwhen the operand is a number. A BigInt operand returns a BigInt.~does not accept a mix of the two types.
Description
The truth table for the NOT operation is:
1337 (base 10) = 0000010100111001 (base 2)
~1337 (base 10) = 1111101011000110 (base 2) = -1338 (base 10)
For an integer already in the signed 32-bit range, ~x equals -(x + 1).
Examples
| value (base 10) | value (base 2) | return (base 2) | return (base 10) |
|---|---|---|---|
| 2 | 10 | 11111100 | -3 |
| 1 | 1 | 11111110 | -2 |
| 0 | 0 | 11111111 | -1 |
| -1 | 11111111 | 0 | 0 |
| -2 | 11111110 | 1 | 1 |
| -3 | 11111100 | 10 | 2 |
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents