I just discovered the bitwise complement unary operation in Python via this question and have been trying to come up with an actual application for it, and if not, to determine if it's generally safe to overload the operator (by overriding the __invert__
method) for other uses. The example given in the question fails with a TypeError
, and the link provided seems pretty intimidating. Here's some fiddling around to see ~
in use:
from bitstring import BitArray
x = 7
print(~x)
# -8
print(BitArray(int=x, length=4).bin)
# '0111'
print(BitArray(int=~x, length=4).bin)
# '1000'
print(~~True, ~~False)
# 1 0
for i in range(-100, 100):
assert i + ~i == -1
assert i ^ ~i == -1
assert bool(i) == ~~bool(i)
Are there any examples of valid use-cases for this operator that I should be aware of? And even if there are, is it generally acceptable to override this operator for types other than int
?
What is the Tilde ~ in Python? Python's Tilde ~n operator is the bitwise negation operator: it takes the number n as binary number and “flips” all bits 0 to 1 and 1 to 0 to obtain the complement binary number. For example, the tilde operation ~1 becomes 0 and ~0 becomes 1 and ~101 becomes 010 .
In mathematics, the tilde operator (Unicode U+223C), sometimes called "twiddle", is often used to denote an equivalence relation between two objects. Thus "x ~ y" means "x is equivalent to y". It is a weaker statement than stating that x equals y.
tilde ~ is a bitwise operator. If the operand is 1, it returns 0, and if 0, it returns 1. So you will get the InvoiceNo values in the df that does not contain the string 'C'
The operator ~ is a binary negation operator (as opposed to boolean negation), and being that, it inverses all the bits of its operand. The result is a negative number in two's complement arithmetic.
The standard use cases for the bitwise NOT operator are bitwise operations, just like the bitwise AND &
, the bitwise OR |
, the bitwise XOR ^
, and bitwise shifting <<
and >>
. Although they are rarely used in higher level applications, there are still some times where you need to do bitwise manipulations, so that’s why they are there.
Of course, you may overwrite these for custom types, and in general you are not required to follow any specific semantics when doing so. Just choose what makes sense for your type and what still fits the operator in some way.
If the operation is obscure and better explained with a word or two, then you should use a standard method instead. But there are some situations, especially when working with number related types, that could have some mathematical-like operations which fit the bitwise operators, and as such are fine to use those.
Just like you would overwrite standard operators like +
and -
only for meaningful operations, you should try to do the same for bitwise operators.
The reason ~~True, ~~False
gives you (1, 0)
is because the bool
type does not define its own __invert__
operation. However, int
does; and bool
is actually a subtype of int
. So bool
actually inherits the logic of all bitwise and arithmetical operators. That’s why True + True == 2
etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With