We Offer 100% Job Guarantee Courses (Any Degree / Diploma Candidates / Year GAP / Non-IT / Any Passed Outs). Placement Records
Hire Talent (HR):+91-9707 240 250

General

Bitwise Operators In Python

Bitwise Operators In Python

Operators are used for performing operations on variables and values. These are considered to be the special symbols that carry out logical and arithmetic computations. The Operand is the operator value that operates on.

Bitwise operators in Python:

In Python, bitwise operators are used for performing bitwise calculations on integers. The numerals are converted to binary, and then bit by bit, the performance is calculated, and therefore the name is derived as bitwise operators. The result is then returned in the format of the decimal. When it comes to Python, the only integer works for the bitwise operators.

OPERATORSYNTAXDESCRIPTION
&a & bBitwise AND
^a ^ bBitwise XOR
~a ~ bBitwise NOT
|a | bBitwise OR
<< a<<Bitwise left shift
>> a>>Bitwise right shift

Now let us explain the usage of each operator with examples.

Bitwise AND operator:

If both the bits are 1, it returns 1 else it returns 0.

For example, consider the following operation of Bitwise AND :

Let x = 4 = 0100
y = 10 = 1010
x & y = 1010 & 0100
= 0000
= 0 (Decimal)

Bitwise XOR operator:

If one of the bit is 1 and the other is given as 0 it returns 1 else it returns 0.

For example, consider the following Bitwise XOR operations :

Let x = 4 = 0100
y = 10 = 1010
x ^ y = 1010 ^ 0100
= 1110
= 14 (Decimal)

Bitwise NOT operator:

One’s complement of the number is returned.

For example, consider the following Bitwise NOT operations :

Let x = 4 = 0100
~x = ~0100
= - (0100 + 1)
= - (0101)
= - 5

Bitwise OR operator:

If one of the bit is 1, it returns 1 else it returns 0.

For example, consider the following Bitwise OR operations :

Let x = 4 = 0100
y = 10 = 1010
x | y = 1010 | 0100
= 1110
= 14 (Decimal)

Python program to illustrate bitwise operators

x = 4
y = 10 
# Print bitwise AND operation         
print("x & y =", x & y)  
# Print bitwise OR operation 
print("x | y =", x | y) 
# Print bitwise NOT operation  
print("~x =", ~x)     
# print bitwise XOR operation  
print("x ^ y =", x ^ y)

Output:

x & y = 0
x | y = 14
~x = -5
x ^ y = 14

Shift Operators:

By multiplying or dividing the given number by 2, the bits of the figure is shifted to left or right by using the Shift operators.

Bitwise left shift:

Bitwise left shift is the same as that of multiplying the number with some power of two. So it fills 0 on voids left as a result, and the bits of the number are shifted to the left.

For example :

x = 5 = 0000 0101
y = -10 = 1111 0110
x << 1 = 0000 1010 = 10
x << 2 = 0001 0100 = 20
y << 1 = 0000 1010 = -20
y << 2 = 0001 0100 = -40

Bitwise right shift:

The bitwise right shift is the same as that of dividing the number with some power of two. So it fills 0 on voids left as a result, and the bits of the number are shifted to the right.

For example:

x = 10
x >> 1 = 5

Python program to illustrate Shift operators :

x = 10
y = -10
# print bitwise right shift operator
print("x >> 1 =", x >> 1)
print("y >> 1 =", y >> 1)
x = 5
y = -10
# print bitwise left shift operator
print("x << 1 =", x << 1)
print("y << 1 =", y << 1)

Output:

x >> 1 = 5
y >> 1 = -5
x << 1 = 10
y << 1 = -20

Bitwise Operator Overloading :

 Extended meaning is given beyond their predefined operational meaning. It is done with the help of Bitwise Operator Overloading.

For example, To add two integer numbers and to join two strings and to merge two lists, we use the “ + “ operator. This is because- for int class and str class “ + “ operator is overloaded. Thus Operator Overloading means that the same built-in operator or function shows various behavior for different class objects.

Python program to illustrate operator overloading

class Gang(): 
def __init__(self, value): 
self.value = value         
def __and__(self, ob): 
print("And Overloaded Operator") 
if isinstance(ob, Gang): 
return self.value & ob.value 
else: 
raise ValueError("Must be a object of class Gang")               
def __or__(self, ob): 
print("Or Overloaded Operator") 
if isinstance(ob, Gang): 
return self.value | ob.value 
else: 
raise ValueError("Must be a object of class Gang")            
def __xor__(self, ob): 
print("Xor Overloaded Operator") 
if isinstance(ob, Gang): 
return self.value & ob.value 
else: 
raise ValueError("Must be a object of class Gang")    
def __lshift__(self, ob): 
print("lshift Overloaded Operator") 
if isinstance(ob, Gang): 
return self.value << ob.value 
else: 
raise ValueError("Must be a object of class Gang")             
def __rshift__(self, ob): 
print("rshift Overloaded Operator") 
if isinstance(ob, Gang): 
return self.value & ob.value 
else: 
raise ValueError("Must be a object of class Gang")               
def __invert__(self): 
print("Invert Overloaded Operator") 
return ~self.value               

# Driver’s code

if __name__ == "__main__": 
x = Gang(10) 
y = Gang(12) 
print(x&y) 
print(x|y) 
print(x ^ y) 
print(x<<y) 
print(x>>y) 
print(~x)

Output:

And Overloaded Operator
8
Or Overloaded Operator
14
Xor Overloaded Operator
8
lshift Overloaded Operator
40960
rshift Overloaded Operator
8
Invert Overloaded Operator
-11

 

I hope the above tutorial helps you to understand clearly about various Bitwise operators in python. Do you have any queries? Ask us through the comment section below.

Related Blogs:

  1. Brief Overview of Python Language
  2. Python Career opportunities
  3. Python Break Continue
  4. Python Control Flow
  5. Python Data Types
  6. Python Dictionary
  7. Python Exception Handling
  8.  Python File
  9. Python Functions
  10. Python Substring

 

Besant Technologies WhatsApp