NumPy Bitwise OperatorsNumpy provides the following bitwise operators.
bitwise_and OperationThe NumPy provides the bitwise_and() function which is used to calculate the bitwise_and operation of the two operands. The bitwise and operation is performed on the corresponding bits of the binary representation of the operands. If both the corresponding bit in the operands is set to 1, then only the resultant bit in the AND result will be set to 1 otherwise it will be set to 0. ExampleOutput: binary representation of a: 0b1010 binary representation of b: 0b1100 Bitwiseand of a and b: 8 AND Truth TableThe output of the AND result of the two bits is 1 if and only if both the bits are 1 otherwise it will be 0.
bitwise_or OperatorThe NumPy provides the bitwise_or() function which is used to calculate the bitwise or operation of the two operands. The bitwise or operation is performed on the corresponding bits of the binary representation of the operands. If one of the corresponding bit in the operands is set to 1 then the resultant bit in the OR result will be set to 1; otherwise it will be set to 0. ExampleOutput: binary representation of a: 0b110010 binary representation of b: 0b1011010 Bitwiseor of a and b: 122 Or truth tableThe output of the OR result of the two bits is 1 if one of the bits are 1 otherwise it will be 0.
Invert operationIt is used to calculate the bitwise not the operation of the given operand. The 2's complement is returned if the signed integer is passed in the function. Consider the following example. ExampleOutput: Binary representation: 00010100 [235] Binary representation: 11101011 It shifts the bits in the binary representation of the operand to the left by the specified position. An equal number of 0s are appended from the right. Consider the following example. ExampleOutput: left shift of 20 by 3 bits 160 Binary representation of 20 in 8 bits 00010100 Binary representation of 160 in 8 bits 10100000 Right Shift OperationIt shifts the bits in the binary representation of the operand to the right by the specified position. An equal number of 0s are appended from the left. Consider the following example. ExampleOutput: left shift of 20 by 3 bits 2 Binary representation of 20 in 8 bits 00010100 Binary representation of 160 in 8 bits 10100000
Next TopicNumPy String Functions
