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.

This function is useful in various applications, including:
1. Image Processing: Manipulating the bits of an image to apply masks or filters by isolating specific color channels or adjusting the transparency of an image.
2. Binary Data Processing: When working with data in binary format, bitwise_and() can be used to extract specific bits of data or combine data from multiple sources.
3. Bit Flags: In some cases, you may want to store multiple boolean values in a single integer using bit flags. The bitwise_and() function can be used to check if specific flags are set.
4. Cryptography: Bitwise operations, such as bitwise AND, are often used in cryptographic algorithms for scrambling and unscrambling data.

Syntax:

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

Parameters:

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: Demonstration of NumPy Bitwise AND Operation

>>> 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])

The above code snippet demonstrates the usage of the NumPy library's bitwise_and() function for performing bitwise AND operations on integers and arrays. It also shows how to obtain the binary representation of an integer using the binary_repr() function.

Example: Bitwise AND Operation with NumPy Arrays

>>> 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)

The above code demonstrates how to perform bitwise AND operations on NumPy arrays. The numpy.bitwise_and() function is used to calculate the element-wise bitwise AND of two input arrays. It accepts lists or NumPy arrays as input and returns a NumPy array with the result. The code also shows an example of using boolean values as input and outputs a boolean array.

Python Code Editor:

Previous: NumPy Binary operation Home
Next: bitwise_or()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/numpy/binary-operations/bitwise-and.php