Python Tutorial

Python Bitwise Operators (Explained With Example)

What are Bitwise Operators in Python?

The bitwise operator works with bits, and performs operations one by one. Assume that a = 60 and b = 13. Their binary values will be 0011 1100 and 0000 1101, respectively. 

The table below lists the bitwise operators available in Python, along with an example for each. We use the aforementioned two variables (a and b) as operands in each of these.

a = 0011 1100

b = 0000 1101

-----------------

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a  = 1100 0011

The following Bitwise operators are supported by the Python language.

 Bitwise Operators in Python Examples

Python bin() method

The bin() method converts and returns the binary equivalent string of a given integer. The prefix 0b represents that the result is a binary string.

Code 

print(bin( 5 ) )
Output:
0b101
Did you find this article helpful?