Why can't I xor strings?

Alex Martelli aleaxit at yahoo.com
Mon Oct 11 03:24:43 EDT 2004


Jeremy Bowers <jerf at jerf.org> wrote:

> On Fri, 08 Oct 2004 16:19:22 -0400, dataangel wrote:
> > Regardless of whether this is the best implementation for detecting if
> > two strings are similar, I don't see why xor for strings shouldn't be
> > supported. Am I missing something?
> 
> The basic problem is that there is no obvious "xor on string" operation
> *in general*, even if you stipulate "bitwise". In particular, what does it
> mean to bitwise xor two different length strings?

What I, personally, would expect here, would be to leave the trailing
part of the longer string "untouched".  I.e., just like:

def bitwisexor(s1, s2):
    result = [ chr(ord(a) ^ ord(b)) for a, b in zip(s1, s2) ]
    # at most one of the following 2 stmts actually does anything:
    result.extend(s1[len(result):])
    result.extend(s2[len(result):])
  
    return ''.join(result)

I realize this is the same as imagining the shorter string to be
conceptually padded with as many '\x0' characters as needed -- that just
seems the only "intuitively natural" way to extend "sequence xor'ing" to
sequences of different length...


Alex



More information about the Python-list mailing list