Re: [Tutor] What is the augmented assignment operator ^=

2007-02-19 Thread Rikard Bosnjakovic
On 2/19/07, Dick Moores [EMAIL PROTECTED] wrote:

 I've tried ^= out a bit:
[...]
 and get that strange alternating behavior. Can someone explain?
 And while at it, please also explain = and |=.

^ is XOR,  is AND, | is OR, all bitwise.

You can read more about them here: http://www.somacon.com/p125.php
under C bitwise operators.


-- 
- Rikard.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is the augmented assignment operator ^=

2007-02-19 Thread Andre Engels

2007/2/19, Dick Moores [EMAIL PROTECTED]:


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 (11 = 1, 10=01=00=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, [EMAIL PROTECTED]
ICQ: 6260644  --  Skype: a_engels
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is the augmented assignment operator ^=

2007-02-19 Thread Dick Moores
At 02:17 AM 2/19/2007, Andre Engels wrote:

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 (11 = 1, 
10=01=00=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

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.?

Also, of what practical use are these things?

Dick


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is the augmented assignment operator ^=

2007-02-19 Thread Kent Johnson
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  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is the augmented assignment operator ^=

2007-02-19 Thread Dick Moores
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  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is the augmented assignment operator ^=

2007-02-19 Thread Andre Engels

2007/2/19, Dick Moores [EMAIL PROTECTED]:


At 02:17 AM 2/19/2007, Andre Engels wrote:

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 (11 = 1,
10=01=00=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

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 111111, 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, [EMAIL PROTECTED]
ICQ: 6260644  --  Skype: a_engels
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is the augmented assignment operator ^=

2007-02-19 Thread Rikard Bosnjakovic
On 2/19/07, Dick Moores [EMAIL PROTECTED] 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/aixcxx/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  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is the augmented assignment operator ^=

2007-02-19 Thread Alan Gauld
Dick Moores [EMAIL PROTECTED] wrote

 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.?

Remember that the digits are 32 bits long so 3 is not  11
but
011

And the representation of negative numbers is more complex
than you might think.

You can see the full hex representations using struct:

 import struct
 struct.pack('i',-3)
'\xfd\xff\xff\xff'
 struct.pack('i',3)
'\x03\x00\x00\x00'

Since most of 3 is zeros the and results will be zro, so
lets look at the significant bits:

 0xfd  0x03
1


voila!

 Also, of what practical use are these things?

Take a look at my Using the OS topic, there is a side-bar(box)
just short of half way down that describes how these can be
used to test file status. There are many other uses too, but
that's one real-world case.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is the augmented assignment operator ^=

2007-02-19 Thread Dick Moores
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  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor