+ 1
Please explain ^ operator on Python.
3 Antworten
+ 8
^ is xor gate operator
5^3 = 9
05 = 00000101b
12 = 00001100b
^
09 = 00001001b
+ 8
It also works for collections, like sets and means eXclusive OR - returns only those items which are in a set A but not B or B but not A.
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
A^B = {1, 2, 5, 6} # 3 and 4 are in both sets