[Python-ideas] String and bytes bitwise operations

Serhiy Storchaka storchaka at gmail.com
Thu May 17 07:14:10 EDT 2018


17.05.18 13:53, Ken Hilton пише:
> We all know the bitwise operators: & (and), | (or), ^ (xor), and ~ 
> (not). We know how they work with numbers:
> 
> 420 ^ 502
> 
> 110100100
> 111110110
> == XOR ==
> 001010010
> = 82
> 
> But it might be useful in some cases to (let's say) xor a string (or 
> bytestring):

The question is how common a need of these operations? If it is not 
common enough, they are better be implemented as functions in a 
third-party library.

> Currently, that's done with this expression for strings:
> 
>      >>> ''.join(chr(ord(a) ^ ord(b)) for a, b in zip('HELLO', 'world'))
>      '?*> +'

Are you aware that this can raise a ValueError for some input strings? 
For example for '\U00100000' and '\U00010000'.



More information about the Python-ideas mailing list