what does ^ do in python

Christian Heimes lists at cheimes.de
Tue Mar 25 23:19:09 CET 2008


Dark Wind schrieb:
> Hi,
> 
> In most of the languages ^ is used for 'to the power of'. In python we have
> ** for that. But what does ^ do?
> I could not get it just by using it ... some examples are:
> 1^1 returns 0
> 2^2 returns 0
> 1^4 returns 5
> 4^1 returns 5
> 3^5 returns 6
> 5^3 returns 6 ......


^ is  exclusive or (xor)

# 1 ^ 1 = 0
>>> 1 ^ 1
0
# 01 ^ 10 = 11
>>> 1 ^ 2
3
# 01 ^ 11 = 10
>>> 1 ^ 3
2
# 001 ^ 100 = 101
>>> 1 ^ 4
5

Christian




More information about the Python-list mailing list