[Tutor] Quick Question
D-Man
dsh8290@rit.edu
Wed, 25 Apr 2001 10:14:25 -0400
On Wed, Apr 25, 2001 at 04:05:48PM +1000, Glen Wheeler wrote:
| Hey guys,
|
| 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?
Yes.
| My question is, what does x^2 do? It seems like x^y === x + y -
| is this true?
It might be -- depends on what the values of x and y are :
>>> x = 3
>>> y = 4
>>> print x+y
7
>>> print x^y
7
>>> x = 2
>>> y = 3
>>> print x+y
5
>>> print x^y
1
<grin>
http://www.python.org/doc/current/ref/bitwise.html#l2h-336
The ^ operator yields the bitwise XOR (exclusive OR) of its arguments,
which must be plain or long integers. The arguments are converted to a
common type.
IIRC this operator is the same in Java. C and C++ don't have a
builtin _logical_ XOR and I don't remember off the top of my head what
the '^' operator does. I think it might be bitwise negation or
bitwise xor, but I don't think I've ever used it.
In the examples above, in case you're not familiar with binary,
3 : 011
4 : 100
7 : 111
2 : 010
3 : 011
1 : 001
1 : true
0 : false
HTH,
-D