# [Tutor] What is the augmented assignment operator "^="

Andre Engels andreengels at gmail.com
Mon Feb 19 11:17:07 CET 2007

```2007/2/19, Dick Moores <rdm at rcblue.com>:
>
> The docs list it at <http://docs.python.org/ref/augassign.html>, and
> send you to <http://docs.python.org/ref/primaries.html#primaries>,
> which seems a dead end.
>
> I've tried "^=" out a bit:
>
> >>> n = 5
> >>> n ^= 8
> >>> n
> 13
> >>> n ^= 8
> >>> n
> 5
> >>> n ^= 8
> >>> n
> 13
> >>> n ^= 8
> >>> n
> 5
>
> and get that strange alternating behavior. Can someone explain?  And
> while at it, please also explain "&=" and "|=".
>

To understand these operators, you will have to think of the numbers as
binary numbers. Look at the digits. For two numbers x and y, x^y is the
effect of doing an exclusive or on all digits (that is, 0^1 = 1^0 = 1 and
0^0 = 1^1 = 0), & of doing an and (1&1 = 1, 1&0=0&1=0&0=0) and | is an or on
all digits (1|1=1|0=0|1 = 1, 0|0 = 0).

So 5^8 = 110 ^ 1000 = 0110 ^ 1000 = 1110 = 13
and 13^8 = 1110 ^ 1000 = 0110 = 5

--
Andre Engels, andreengels at gmail.com
ICQ: 6260644  --  Skype: a_engels
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070219/0ca2aed0/attachment-0001.htm
```