List Info

Thread: What is the augmented assignment operator "^="




What is the augmented assignment operator "^="
country flaguser name
United States
2007-02-19 03:45:09
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
"|=".

Thanks,

Dick Moores

_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor

Re: What is the augmented assignment operator "^="
country flaguser name
United States
2007-02-19 05:32:25
Dick Moores wrote:
> 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.

a += n is more-or-less a shortcut for a = a + n. There are a
few 
subtleties which the first page you reference talks about,
but you can 
generally think of it as a handy abbreviation.

For other operations, the same is true:
a <op>= n is the same as a = a <op> n

> I've tried "^=" out a bit and get that
strange alternating behavior.

which is normal operation of ^. Try it with + or * for a
simpler example.

Kent

_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor

Re: What is the augmented assignment operator "^="
country flaguser name
United States
2007-02-19 05:40:20
At 03:32 AM 2/19/2007, you wrote:
>Dick Moores wrote:
>>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.
>
>a += n is more-or-less a shortcut for a = a + n. There
are a few 
>subtleties which the first page you reference talks
about, but you 
>can generally think of it as a handy abbreviation.
>
>For other operations, the same is true:
>a <op>= n is the same as a = a <op> n
>
>>I've tried "^=" out a bit and get that
strange alternating behavior.
>
>which is normal operation of ^. Try it with + or * for a
simpler example.

Of those listed on <http://
docs.python.org/ref/augassign.html>,  I 
already use and understand "+=" | "-=" |
"*=" | "/=" | "%=" |
"**=" . 
It's the remaining seven I'm wondering about, or really
about >>, <<, 
&, ^, and | . Andre's given me a good start, as you may
have seen by now.

Dick


_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor

Re: What is the augmented assignment operator "^="
user name
2007-02-19 05:46:08
2007/2/19, Dick Moores < rdmrcblue.com">rdmrcblue.com>:
At 02:17 AM 2/19/2007, Andre Engels wrote:

&gt;To understand these operators, you will have to think of the numbers
&gt;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&amp;0=0&1=0&0=0) and | is an or on all digits (1|1=1|0=0|1 = 1, 0|0 = 0).
>
&gt;So 5^8 = 110 ^ 1000 = 0110 ^ 1000 = 1110 = 13
>and 13^8 = 1110 ^ 1000 = 0110 = 5

Thanks, Andre! I've got it for the three operators, for non-negative
integers. But I'm not sure I understand how negative integers work.
For example, is 3 & -3 = 1
because it is 11 & -11 = 01, and that's because one of the first
digits of 11 and -11 is not 1, and both of their 2nd digits ARE 1, Q.E.D.?

This has to do with the internal representation of negative numbers: -1 is represented as 111....111, with one 1 more than maxint. -2 is 111...110 and -3 is 111...101. Thus 3 & -3 is 11 & 111...101 = 1

Also, of what practical use are these things?

I guess they have their uses for people accustomed to dealing with hardware. Apart from that, you can get a very space-efficient representation for multiple boolean variables. If you have what looks like an array of booleans, you could also represent it as a single natural number, for example [true, true, false, false, true, false] would then be 1*1 + 1*2 + 0*4 + 0*8 + 1*16 + 0*32 = 19. Using such a representation and the above operators would enable one to write

z = x & y

instead of

z = [x[i] and y[i] for i in range(len(x))]

when modelling the same using lists.

Of course this does come at the price of complicating

x[i] = true

to

x |= 2 ** i

which though not really longer does definitely look harder to understand.

--
Andre Engels, andreengelsgmail.com">andreengelsgmail.com
ICQ: 6260644&nbsp; --  ;Skype: a_engels
Re: What is the augmented assignment operator "^="
user name
2007-02-19 05:45:27
On 2/19/07, Dick Moores <rdmrcblue.com> wrote:

> It's the remaining seven I'm wondering about, or really
about >>, <<,
> &, ^, and | .

This webpage will tell you - in detail - about all the
operators:
http://www.lnf.infn.it/Calcolo/doc/aixcx
x/html/language/ref/ruclxbin.htm

The bitwise operators are hard to understand without proper
knowledge
about the binary number system. I recommend you to read up
about it,
if you feel the operators are somewhat difficult to
understand.


-- 
- Rikard.
_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor

Re: What is the augmented assignment operator "^="
country flaguser name
United States
2007-02-19 09:07:57
My sincere thanks to Rikard Bosnjakovic, Andre Engels, and
Alan 
Gauld. I think you've given me a good start toward
understanding the 
operators >>, <<,  &, ^, and  | ; 32-bit
numbers, and negative binary numbers.

Dick Moores


_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor

[1-6]

about | contact  Other archives ( Real Estate discussion Medical topics )