## 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 known as ‘Programs’ are assisted to carry 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 true meaning of the Python Bitwise Operators. Bitwise Operators are a group of operators that are used to manipulate or perform operations on operands bit by bit rather 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.

**Note:**one crucial thing to note regarding these operators is that they work only on integers and return the output in decimal format.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.

4.8 (4,742 ratings)

View Course

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

0 | 0 | 0 | 0 | 0 |

0 | 1 | 0 | 1 | 1 |

1 | 0 | 0 | 1 | 1 |

1 | 1 | 1 | 1 | 0 |

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

**Syntax**

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 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 the examples
- Right Shift – Refer the examples

### 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 it both the bits are 0 it will return 0 as 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 bits present at the right side are removed.

**AND**

`A = 10 =>1010(Binary)`

B = 7 => 111(Binary)

A&B = 1010 & 0111

= 0010

= 2 (Decimal)**OR**

`A = 10 =>1010(Binary)`

B = 7 => 111(Binary)

A I B = 1010 I 0111

= 1111

= 15 (Decimal)**XOR**

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

### Conclusion

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.

### Recommended Articles

This is a guide to 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 –