Python Bitwise Operators - Binary OR, Binary XOR, Binary Left shift, Binary Right Shift
<Logical Operators Identity Operators >
- Bitwise operators can operate on binary numbers.
- It considers their operand as a sequence of bits (0's or 1's)
- They can perform the operations on the operands as bit-by-bit operations.
- Return the results as python numerical values.
Following is the list of bitwise operators.
Operators | Meaning |
---|---|
Binary OR (|) | Returns the result as 1, only If each bit position of either or both operands is 1. |
Binary AND(&) | Returns the result as 1, only If each bit positions both operands are 1. |
Binary XOR (^) | Returns the result as 1, only If each bit position of either but not both operands is 1. |
Binary 1's (~) | Complement returns the invert of given operands bits. |
Binary Left shift (<<) | left side value is shifted to the left by the number of bits mentioned in the right side operand. |
Binary Right Shift (>>) | left side value is shifted to the right by the number of bits mentioned in the right side operand. |
Example Program with Bitwise Operators
>>a=10
>>b=5
>> # shows how to use the Binary OR(|) operator
>> a|b
15
>> # shows how to use the Binary AND(&) operator
>> a&b
0
>> # shows how to use the Binary XOR(^) operator
>> a^b
15
>> # shows how to use the Binary 1's Complement
>> ~a
-11
>> ~b
-6
>> ~1
-2
>> # shows how to use the Binary Left Shift(<<) operator
>> a<<2
40
>> b<<2
20
>> # shows how to use the Binary Right Shift(>) operator
>> a>2
2
>> a>2
2
>> b>2
1
<Logical Operators Identity Operators >