Non-POSIX parity (mark/space) with Python-Serial on Linux.

Grant Edwards invalid at invalid.invalid
Mon Nov 21 14:42:35 EST 2011


On 2011-11-21, Matthew Lenz <matthew at nocturnal.org> wrote:

> Another thing I noticed is that the & and | appear to give the same
> result as adding or subtracting 128 from the ordinal value.

Nope, that's only true for some values.

If we're limiting ourselves to byte values, then we're talking
modulo-256 arithmetic, so:

128 + 128 = 0 
128 | 128 = 128

0 - 128  = 128
0 & 0x7f = 0


What's is true is that adding 128 is actullay the same as subtracting
128, and both are the same as exclusive-or 128 (v ^ 128):

>>> x = 128
>>> (x + 128) & 0xff
0
>>> (x - 128) & 0xff
0
>>> (x ^ 128) & 0xff
0

>>> x = 0
>>> (x + 128) & 0xff
128
>>> (x - 128) & 0xff
128
>>> (x ^ 128) & 0xff
128

> I'm assuming that isn't coincidence. :)

Well, the weighting of the high-order bit in an 8-bit wide binary
number is 128, if that's what you're getting at...

-- 
Grant Edwards               grant.b.edwards        Yow! How's it going in
                                  at               those MODULAR LOVE UNITS??
                              gmail.com            



More information about the Python-list mailing list