[Tutor] bitwise ops in Idle (fwd)

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Nov 21 15:33:18 EST 2003


Hi Vicki,

> Then my problem may be that I am trying to do it with hexadecimal
> numbers. I tried this first:
>
> '\x5E' ^ '\x09'


XOR works between ints, not strings, so we need to first transform those
strings into ordinals:

###
>>> ord('\x5e') ^ ord('\x09')
87
>>> hex(87)
'0x57'
###

It sounds that, as you read those bytes from your port, you'll want to
convert each byte internally into an integer by using ord().


Good luck to you!


---------- Forwarded message ----------
Date: Thu, 20 Nov 2003 13:36:29 -0500
From: "Stanfield, Vicki {D167~Indianapolis}" <VICKI.STANFIELD at roche.com>
To: Danny Yoo <dyoo at hkn.eecs.berkeley.edu>
Subject: RE: [Tutor] bitwise ops in Idle

Then my problem may be that I am trying to do it with hexadecimal
numbers. I tried this first:

'\x5E'^'\x09'

and it failed leading me to look for a module. How does one (or does
one) do an XOR of hex numbers?

--vicki

-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On
Behalf Of Danny Yoo
Sent: Thursday, November 20, 2003 1:28 PM
To: Vicki Stanfield
Cc: tutor at python.org
Subject: Re: [Tutor] bitwise ops in Idle




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!


_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor




More information about the Tutor mailing list