Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Python by (19k points)
Hi all,

So basically my requirement is pretty simple. I just wanted to know I could make use of the tilde operator (~) to invert a Numpy array consisting of boolean data. However, I am aware of the standard way to do it is to use the functions np.invert() and even np.logical_and().

Tilde seems to work but my concern here is that I am unable to find the plausible usage in any Numpy reference material, to be honest.

Note: I have seen that it does not work with scalars at all. Well this is what I mean, consider this example:

Example: bool(~True) -----> This returns True.

Appreciate all the help!Hi all,

So basically my requirement is pretty simple. I just wanted to know I could make use of the tilde operator (~) to invert a Numpy array consisting of boolean data. However, I am aware of the standard way to do it is to use the functions np.invert() and even np.logical_and().

Tilde seems to work but my concern here is that I am unable to find the plausible usage in any Numpy reference material, to be honest.

Note: I have seen that it does not work with scalars at all. Well this is what I mean, consider this example:

Example: bool(~True) -----> This returns True.

Appreciate all the help!

1 Answer

0 votes
by (33.1k points)

Here, the bitwise_not is an alias for the invert method:

For example:

>> np.bitwise_not is np.invert

>> True

In general, the Bit-wise NOT operator of the binary representation of the integers is present in the array input.

Well, the ufunc implements the Python Operator Tilde (~).

Browse Categories

...