NumPy Binary operations: bitwise_xor() function
numpy.bitwise_xor() function
The bitwise_xor() function is used to compute the bit-wise XOR of two arrays element-wise.
Some common applications of numpy.bitwise_xor() include:
1. Cryptography: Applying XOR operations on binary keys or data during encryption or decryption processes, such as stream ciphers or one-time pad algorithms.
2. Error Detection and Correction: Implementing error detection and correction algorithms that involve XOR operations.
3.
Digital Signal Processing: Performing bitwise XOR operations on digital signals for various purposes, such as data scrambling or descrambling in communication systems.
Syntax:
numpy.bitwise-xor(x1, x2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj ]) = <ufunc 'bitwise-xor'>
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: Bitwise XOR operation with NumPy and binary representation
>>> import numpy as np
>>> np.bitwise_xor(12, 16)
28
>>> np.binary_repr(28)
'11100'
In the above example, the bitwise XOR of two integers, 12 and 16, is calculated, resulting in 28. Then, the numpy.binary_repr() function is used to display the binary representation of the result (28), which is '11100'.
Example: Bitwise XOR operation with NumPy on integers and arrays
>>> import numpy as np
>>> np.bitwise_xor(28, 5)
25
>>> np.bitwise_xor([28, 3], 7)
array([27, 4])
In the first example of the above code, the bitwise XOR of two integers, 28 and 5, is calculated, resulting in 25. In the second example, the bitwise XOR operation is performed between an array of integers ([28, 3]) and a single integer (7), resulting in a new array with the element-wise XOR applied ([27, 4]).
Example: Bitwise XOR operation with NumPy arrays and boolean values
import numpy as np
>>> np.bitwise_xor([31,3], [5,6])
array([26, 5])
>>> np.bitwise_xor([True, True], [False, True])
array([ True, False], dtype=bool)
In the first example of the above code, the bitwise XOR operation is performed between two arrays of integers ([31, 3] and [5, 6]), resulting in a new array with the element-wise XOR applied ([26, 5]). In the second example, the bitwise XOR operation is performed between two arrays of boolean values ([True, True] and [False, True]), resulting in a new boolean array with the element-wise XOR applied ([True, False]).
Python Code Editor:
Previous: bitwise_or()
Next: invert()
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-xor.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics