Example of Bitwise Operators in Python.
#Bitwise
Operators with variables
a = 60 # 60 = 0011
1100
b = 13 # 13 = 0000
1101
c = 0
print("\nEXAMPLE
OF BITWISE OPERATORS")
print("============================")
print("\nBitwise
AND (&) Operator")
c = a &
b; # 12 =
0000 1100
print ("c =
a & b gives Value of c to :", c)
print("\nBitwise
AND (&) Operator")
c = a |
b; # 61 =
0011 1101
print ("c =
a | b gives Value of c to :", c)
print("\nBitwise
XOR (^) Operator")
c = a ^
b; # 49 =
0011 0001
print ("c =
a ^ b gives Value of c to :", c)
print("\nBitwise
1's Complement (~) Operator")
c =
~a; #
-61 = 1100 0011
print ("c =
~a gives Value of c to :", c)
print("\nBitwise
Left Shift (<<) Operator")
c = a
<< 2; # 240 = 1111 0000
print ("c =
a << 2 gives Value of c to:", c)
print("\nBitwise
Left Shift (>>) Operator")
c = a
>> 2; # 15 = 0000 1111
print
("c = a >> 2 gives Value of c to:", c)
Output
>>>
The
Initialized value of A is =7
The
Initialized value of B is =4
Bitwise
AND Operator : A & B =4
Bitwise
XOR Operator : A ^ B =3
Bitwise
OR Operator : A | B =7
The
Initialized value of A is =14
The
Initialized value of B is =6
Bitwise
AND Operator : A & B =6
Bitwise
XOR Operator : A ^ B =8
Bitwise
OR Operator : A | B =14
>>>
No comments
Post your comments