[Tutor] Quick Questiony

Michael P. Reilly arcege@speakeasy.net
Wed, 25 Apr 2001 10:07:09 -0400 (EDT)


Glen Wheeler wrote
>   I've been writing up a bunch of graphing tools in python...just for =
> fun, and on paper I write x squared as x^2 - this is normal for me.
>   However, in python, it is x**2 - right?
>   My question is, what does x^2 do?  It seems like x^y =3D=3D=3D x + y - =
> is this true?

Not quite.  The '^' operator is 'exclusive or' in binary arithmatic.
Bits in the binary representation of the number are merged together,
but where two bits are both one, the result is zero.

Since computer programs in the past were far more inclined to binary
arithmatic than more complex maths, the "and" (&), "or" (|), "not" (~)
and "exclusive or" (^) operations are more prevalent than "power of"
(which is "**", but also the pow() function), "square root" (sqrt())
and "absolute value" (which could be "|x|", but is abs(x)), or even the
factorial ("!").

However, as you get into graphics, you'll find that a "exclusive
or" operator is far more powerful and useful to you than "power of".
Moving an object within a bitmap is often accomplished using the exlusive
or of the objects bitmap and the background (well, from what I remember
of my graphics course years ago anyway ;).

  -Arcege

-- 
+----------------------------------+-----------------------------------+
| Michael P. Reilly                | arcege@speakeasy.net              |