laziness, impatience, and hubris PerlMonks

### Re: Re: Detecting transpositions

by dash2 (Hermit)
 on Aug 06, 2003 at 16:18 UTC ( #281469=note: print w/replies, xml ) Need Help??

in reply to Re: Detecting transpositions

Very cool. Can you explain why it works? What makes the XOR come out to be like that?
 A massive flamewar beneath your chosen depth has not been shown here

Replies are listed 'Best First'.
Re: Detecting transpositions
by Abigail-II (Bishop) on Aug 07, 2003 at 09:50 UTC
Well, if you do an XOR of two strings (scalars that haven't been used as numbers), perl will do a bitwise XOR on the characters of the strings, instead of the bits on the numbers. Two characters that are the same will have all the bits the same, XORing those characters will give you the character of which all the bits are 0, aka "\x00". If two characters are not the same, there is at least one bitpositions where the bits are different - resulting in a 1 bit when XORing. Hence, the resulting character will be anything but "\x00".

Abigail

Re: Re: Re: Detecting transpositions
by sgifford (Prior) on Aug 07, 2003 at 02:13 UTC
It works because the xor of two bits is zero if and only if the two bits are the same:
```0 ^ 0 = 0
1 ^ 1 = 0
0 ^ 1 = 1
1 ^ 0 = 1
```
So the xor of two characters (which are a string of 8 bits) will be 0 (8 0-bits) if and only if the two characters are the same.

Using xor on strings xors the characters from the first string with the characters in the second string, so in the places where the characters are identical the resulting character will be zero, and in all places where they differ it will be nonzero.

Create A New User
Node Status?
node history
Node Type: note [id://281469]
help
Chatterbox?
 [1nickt]: chacham I have wanted to, and dones so, and so stated in a reply, and been roundly downvoted 2-1. (I assume for attempting to censor.) [1nickt]: pryrt yes, I see. There is no way to differentiate without forcing to a string and all sort of weird stuff. It is enough. User 42 can get creative if s/he wishes. [chacham]: i sent him a private message expressing my displeasure.

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (15)
As of 2017-05-24 20:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favorite model of computation is ...

Results (186 votes). Check out past polls.