What is function of ^ operator in Python


In Python, ^ is called EXOR operator. It is a bitwise operator which takes bits as operands. It returns 1 if one operand is 1 and other is 0.

Assuming a=60 (00111100 in binary) and b=13 (00001101 in binary) bitwise XOR of a and b returns 49 (00110001 in binary)

>>> a=60
>>> bin(a)
'0b111100'
>>> b=a^2
>>> bin(b)
'0b111110'
>>> a=60
>>> bin(a)
'0b111100'
>>> b=13
>>> bin(b)
'0b1101'
>>> c=a^b
>>> bin(c)
'0b110001'

Updated on: 26-Feb-2020

283 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements