The ^ operator

Steve Holden steve at holdenweb.com
Mon Sep 26 14:32:25 EDT 2005


Tuvas wrote:
> What exactly does the ^ operator do? I've seen, for example, that
> 3^4=7, 3^5=8. but 3^3=0. Is it just adding them together if they are
> not equal, and if they are equal, outputs 0, or what? Thanks!
> 

^ is the "bit XOR" operation. It treats its left and right operands as 
binary numbers: the result has a one-bit in those bit positions where 
one operand has a zero and the other has a one.

A B | A XOR B
----+--------
0 0 |    0
0 1 |    1
1 0 |    1
1 1 |    0

If you want exponentiation, try **.

  >>> 3**4
81
  >>> 3**5
243
  >>> 3**6
729
  >>> 3**7
2187
  >>>

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                          www.pycon.org




More information about the Python-list mailing list