w3resource

NumPy Binary operations: bitwise_and() function

numpy.bitwise_and() function

The bitwise_and() is used to compute the bit-wise AND of two arrays element-wise.

Version: 1.15.0

Syntax:

numpy.bitwise_and(x1, x2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None,
subok=True[, signature, extobj ]) = <ufunc 'bitwise_and'>

Parameter:

Name Description Required /
Optional
x1,x2 Only integer and boolean types are handled. Required
out A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. Optional
where Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone. Optional
**kwargs For other keyword-only arguments.

Return value:

out: [ndarray or scalar]
Result: This is a scalar if both x1 and x2 are scalars.

Example-1: numpy.bitwise_and() function

>>> import numpy as np
>>> np.bitwise_and(18, 12)
0
>>> np.bitwise_and(18, 17)
16
>>> np.binary_repr(16)
'10000'
>>> np.bitwise_and([18, 7], 17 )
array([16,  1])

Example-2: numpy.bitwise_and() function

>>> import numpy as np
>>> np.bitwise_and([15, 9], [3, 22])
array([3, 0])
>>> np.bitwise_and(np.array([3, 7, 356]), np.array([4,15,18]))
array([0, 7, 0])
>>> np.bitwise_and([True, True], [False, True])
array([False,  True], dtype=bool)

Python Code Editor:

Previous: NumPy Binary operation Home
Next: bitwise_or()