~

expr unary bit-wise negation operator

Argument is an integer, result is an integer

Bit n of the result is 1 if bit n of the argument is 0. Bit n of the result is 0 if bit n of the argument is 1.

When applied to a negative number, the ~ operator is defined that ~$n==(-1-$n). It is easy to show that this rule applies to any signed twos-complement integer, with the single exception of a number consisting of a 1 in the sign bit, and all 0's elsewhere. This exceptional number (for example, -0x80000000 in a 32-bit word) has an additive inverse that cannot be represented in a word of the same length. Since the (-1-$n) rule applies in any finite-length word, it appears to be the least surprising thing to do for Tcl's arbitrary-length integers.

DGP Just to extend that a bit... If ~$n == (-1-$n) for negative $n, and we also have the property that ~~$n == $n, we can conclude that ~$n is just another way to write -1-$n for all integer values of $n, positive, negative, or 0.

For logical "not" use the ! operator.


AMG: Does Tcl run on any non-two's-complement machines? If so, does it take pains to ensure that ~$n==(-1-$n)?