Bitwise Operators


In Python, bitwise operators are used to perform bitwise calculations on integers. The integers are first converted into binary and then operations are performed on bit by bit, hence the name bitwise operators. Then the result is returned in decimal format. Bitwise AND operator: Returns 1 if both the bits are 1 else 0


OperatorDescription
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise NOT
<< Left shift
>> Right shift

Source Code :1

print("Bitwise Operators Example : Bitwise AND (&)")
number_1=int(input("Enter number 1 : "))
number_2=int(input("Enter number 2 : "))
print(number_1,"&",number_2,"=",number_1&number_2)
To download raw file Click Here

Output :1

Bitwise Operators Example : Bitwise AND (&)
Enter number 1 : 4
Enter number 2 : 2
4 & 2 = 0

Source Code :2

print("Bitwise Operators Example : Bitwise OR (|)")
number_1=int(input("Enter number 1 : "))
number_2=int(input("Enter number 2 : "))
print(number_1,"|",number_2,"=",number_1|number_2)
To download raw file Click Here

Output :2

Bitwise Operators Example : Bitwise OR (|)
Enter number 1 : 3
Enter number 2 : 5
3|5 =7

Source Code :3

print("Bitwise Operators Example : Bitwise XOR (^)")
number_1=int(input("Enter number 1 : "))
number_2=int(input("Enter number 2 : "))
print(number_1,"^",number_2,"=",number_1|number_2)
To download raw file Click Here

Output :3

Bitwise Operators Example : Bitwise XOR (^)
Enter number 1 : 10
Enter number 2 : 2
10 ^ 2 = 10