[Python-ideas] String and bytes bitwise operations

Steve Barnes gadgetsteve at live.co.uk
Fri May 18 05:34:43 EDT 2018



On 18/05/2018 07:07, Greg Ewing wrote:
> Steven D'Aprano wrote:
>> But XORing bytes seems perfectly reasonable. Bytes are numbers, even 
>> if we display them as ASCII characters.
> 
> Yep. Implement it for bytes, then the user can decode/encode
> as appropriate for the application.
> 

Personally I would suggest that the bitwise operations would only make 
sense between pairs of characters, (or bytes), of equal size. (It is 
possible to make a rule that the returned value is the same size as the 
larger of the two - i.e. the smaller is promoted and left padded with 
0s). But such operations don't make much sense in most cases.

However the bitwise operators could sensibly be used for set-wise 
operations on strings, i.e.:
  - "123xyz" | "abc" = "123abcxyz",
  - "Spam" & "Eggs" = "", "Spam" & "Scam" = "Sam"
  - "Spam" ^ "Scam" = "cp"

This __might__ have some practical use?
-- 
Steve (Gadget) Barnes
Any opinions in this message are my personal opinions and do not reflect 
those of my employer.

---
This email has been checked for viruses by AVG.
http://www.avg.com



More information about the Python-ideas mailing list