In [1]:
import numpy as np

In [2]:
print(np.__version__)


1.17.3

In [3]:
a_int = np.array([0, 1, 3])  # [0b00 0b01 0b11]
b_int = np.array([1, 0, 2])  # [0b01 0b00 0b10]

In [4]:
print(a_int.dtype)


int64

In [5]:
print(b_int.dtype)


int64

In [6]:
print(a_int & b_int)


[0 0 2]

In [7]:
print(a_int | b_int)


[1 1 3]

In [8]:
print(a_int ^ b_int)


[1 1 1]

In [9]:
print(~a_int)


[-1 -2 -4]

In [10]:
print(a_int << b_int)


[ 0  1 12]

In [11]:
print(a_int >> b_int)


[0 1 0]

In [12]:
print(np.bitwise_and(a_int, b_int))


[0 0 2]

In [13]:
print(np.bitwise_or(a_int, b_int))


[1 1 3]

In [14]:
print(np.bitwise_xor(a_int, b_int))


[1 1 1]

In [15]:
print(np.bitwise_not(a_int))


[-1 -2 -4]

In [16]:
print(np.invert(a_int))


[-1 -2 -4]

In [17]:
print(~a_int)


[-1 -2 -4]

In [18]:
print(-(a_int + 1))


[-1 -2 -4]

In [19]:
a_uint8 = np.array([0, 1, 3], dtype=np.uint8)

In [20]:
print(~a_uint8)


[255 254 252]

In [21]:
a_uint16 = np.array([0, 1, 3], dtype=np.uint16)

In [22]:
print(~a_uint16)


[65535 65534 65532]

In [23]:
c_int_2d = np.arange(6).reshape(2, 3)
print(c_int_2d)


[[0 1 2]
 [3 4 5]]

In [24]:
print(c_int_2d & a_int)


[[0 1 2]
 [0 0 1]]

In [25]:
print(np.bitwise_and(c_int_2d, a_int))


[[0 1 2]
 [0 0 1]]

In [26]:
print(c_int_2d & 2)


[[0 0 2]
 [2 0 0]]

In [27]:
print(np.bitwise_and(c_int_2d, 2))


[[0 0 2]
 [2 0 0]]

In [28]:
d_float = np.array([0, 1, 3], dtype=float)

In [29]:
# print(a_int & d_float)
# TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

In [30]:
# print(~d_float)
# TypeError: ufunc 'invert' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

In [31]:
# print(np.bitwise_and(a_int, d_float))
# TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

In [32]:
# print(np.bitwise_not(d_float))
# TypeError: ufunc 'invert' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

In [33]:
e_bool = np.array([True, False, True])

In [34]:
print(a_int & e_bool)


[0 0 1]

In [35]:
print((a_int & e_bool).dtype)


int64

In [36]:
print(np.bitwise_and(a_int, e_bool))


[0 0 1]

In [37]:
print(np.bitwise_and(a_int, e_bool).dtype)


int64

In [38]:
print(~e_bool)


[False  True False]

In [39]:
print(np.logical_not(e_bool))


[False  True False]

In [40]:
print(np.bitwise_not(e_bool))


[False  True False]

In [41]:
print(np.invert(e_bool))


[False  True False]