Updated March 16, 2023
Introduction to Python Bitwise Operator
There are a number of related things before we can move ahead in this article considering the ‘Python Bitwise Operators’. This will also be a better approach; otherwise, things will get more skeptical.
Bitwise – Computer programming, which we all know as ‘Programs’, are assisted in carrying out some arithmetic and logical operations. So, these programs work on the level of one or more-bit patterns. The need for this is used to manipulate values for comparisons and calculations.
Operators – Refers to any kind of symbol that indicates any operations to be performed.
Explanation (different Python bitwise operator)
As we have worked on the fundamental part, let us move to the python approach and try to find the Python Bitwise Operators’ true meaning. Bitwise Operators are a group of operators that are used to manipulate or perform operations on operands bit by bit rather than all at once.
These are performed on the integers, which are later converted to the binary format, where the operations are performed bit by bit.
Let us understand what are Decimal or base 10 numbers and Binary or Base 2 numbers, and then we will continue with the operator’s explanations.
- Decimal or Base 10 – Decimals are the numbers that we give the program as an input so as to perform an operation. These are understandable by a human. You may also think of Decimals as the numbers that we use in day to day life (i.e. 0 to 9). It is also known as base 10 because there are 10 numbers between 0 to 9.
- Binary or Base 2 – 0’s and 1’s make the binary numbers; these are understandable by the computers. So all the binary numbers are made of just these, i.e. 0’s and 1’s and thus called base 2 numbers.
Let us take X = 9 and Y = 65 and carry out the calculations. So the binary values will be X = 0110 and Y = 1000.
Let us see the truth table –
|x||y||x & y||X | y||x ^ y|
So here, we have provided the commands in consideration to all the bitwise operators.
>>> a = 9 >>> b = 65 >>> print("Bitwise AND Operator On 9 and 65 is = ", a & b) >>> print("Bitwise OR Operator On 9 and 65 is = ", a | b) >>> print("Bitwise EXCLUSIVE OR Operator On 9 and 65 is = ", a ^ b) >>> print("Bitwise NOT Operator On 9 is = ", ~a) >>> print("Bitwise LEFT SHIFT Operator On 9 is = ", a << 1) >>> print("Bitwise RIGHT SHIFT Operator On 65 is = ", b >> 1)
The output will be something like this –
>>> a = 9
>>> b = 65
>>>print(“Bitwise AND operator on 9 and 65 is = “,a & b)
Bitwise AND operator on 9 and 65 = 1
>>>print(“Bitwise OR operator on 9 and 65 is = “,a | b)
Bitwise OR operator on 9 and 65 = 73
>>>print(“Bitwise EXCLUSIVE OR operator on 9 and 65 is = “,a ^ b)
Bitwise EXCLUSIVE OR operator on 9 and 65 = 72
>>>print(“Bitwise NOT operator on 9 is = “, ~a)
Bitwise NOT operator on 9 is = -10
>>>print(“Bitwise LEFT SIFT operator on 9 is =”, a<<1)
Bitwise LEFT SHIFT operator on 9 is = 18
>>>print(“Bitwise RIGHT SHIFT operator on 65 is =”, b>>1)
Bitwise RIGHT SHIFT operator on 65 is = 32
As of now, we have got a brief idea about the Bitwise operators in Python. To see what the applying syntax to these operators is, let us highlight first their types followed by their syntax.
Types of bitwise operators –
- Complements – Refer to the examples
- And – 4 & 8 (4 = 0100 and 8 = 1000; so, AND value will be 0000, which is 0). Hence the output.
- OR – (0l0 = 0; 0I1 = 1; 1I0 = 1; 1I1 =1)
- XOR – (0^0 = 0; 0^1 = 1; 1^0 = 1; 1^1 = 0)
- Left Shift – Refer to the examples
- Right Shift – Refer to the examples
Let us see some examples that will boost our understanding –
- Bitwise AND operators – if both the comparing bits are 1, then the Bitwise AND will return 1 otherwise 0.
- Bitwise OR Operators – if both the comparing bits are 1, it will return 1, or if both the bits are 0, it will return 0 as a value.
- Bitwise XOR Operators – the XOR operator will return 1 as a value if any of the bit is 0 or 1. But if both the bots are either 0 or 1, then it will return 0.
- Bitwise One’s Compliment Operators – The bitwise One’s operator for A will be –(A+1).
- Bitwise left shift Operators – In the bitwise left shift operator, the binary number is appended with complying 0’s at the end.
- Bitwise right shift Operators – In the bitwise right shift operators, the right side’s bits are removed.
A = 10 =>1010(Binary) B = 7 => 111(Binary) A&B = 1010 & 0111 = 0010 = 2 (Decimal)
A = 10 =>1010(Binary) B = 7 => 111(Binary) A I B = 1010 I 0111 = 1111 = 15 (Decimal)
A = 10 =>1010(Binary) B = 7 => 111(Binary) A ^ B = 1010 ^ 0111 = 1101 = 13 (Decimal)
- Bitwise ONE’S Complement
A = 10 =>1010(Binary) ~A = ~1010 = -(1010+1) = -(1011) = -11(Decimal)
- Bitwise Left Shift
A = 10 =>1010(Binary) A<<2 = 1010<<2 = 101000 = 40(Decimal)
- Bitwise Right Shift
A = 10 =>1010(Binary) A>>2 = 1010>>2 =10 = 2(Decimal)
The bitwise operators are just a small part of the vast learning curve what Python offers to its users. One more thing regarding the bitwise operators is that they are not used frequently in real-world programming. You will only come across these operators’ concepts when you are into works related to encryption, compression, and byte manipulation.
This is a guide to the Python Bitwise Operator example. Here we discuss the explanation of different Python Bitwise Operator with examples and syntax. You may also look at the following article to learn more –