[Tutor] bitwise ops in Idle
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Thu Nov 20 13:28:20 EST 2003
On Thu, 20 Nov 2003, Vicki Stanfield wrote:
> I am trying to get the value of a reiterative XOR operation and thought
> I'd use Idle to figure it out.
Hi Vicki,
You can use the exclusive or (XOR) bitwise operator: '^'. For example:
###
>>> 8 ^ 7
15
>>> (8 ^ 7) ^ 7
8
###
The 'operator' module,
http://www.python.org/doc/lib/module-operator.html
also contains XOR, but exposes it as a function instead of a built-in
operator. We can access it like this:
###
>>> import operator
>>> operator.xor(8, 7)
15
>>> operator.xor(operator.xor(8, 7), 7)
8
###
Hope this helps!
More information about the Tutor
mailing list