Live data from Hacker News

The Three Ways of XOR

horia141.com

51–60 of 69 posts

Re: The Three Ways of XOR

#51
post #49
post #45

Earlier quoted context omitted.

You can do the same with addition/subtract. Perhaps there is a simpler way, but here is one way to do it: a = a + b b = b + a a = b - a b = b - 2*a

You have to take overflow into account. In most cases, that would make the algorithm not very useful compared to just doing the swap with a third variable.

Hm I don't think overflow will apply. Anything it does in an add will be undone by a subtract?

Except that 2a, that might be trouble.

Re: The Three Ways of XOR

#52

Very early on in my computing career (nearly 40 years ago now), I remember being blown away when an older IBM Systems 360 programmer showed me how you can swap the values of two variables over WITHOUT using a third placeholder variable by using pure XOR. I didn't believe him until he showed me. Apparently they used to use it all the time to swap out entire segments of RAM in the S/360 without having to page out to di…

This is a classic trick, and as you write could be used for performance benefits in the old day.

However, the semantics differ from just using a temporary variable in that if a and b are in the same memory location then the result will be zero.

This was used in an entry for the underhanded C contest [1] if I remember correctly where for an implementation of RC4 the author defined the following macro.

    #define SWAP(x, y) do { x^=y; y^=x; x^=y; }
And used it for swapping the values in the substitution table for the cipher, e.g. SWAP(S[i], S[j]). The weakness was that since sometimes the indices are the same in RC4 the substitution table would be gradually replaced with zeroes.

[1] http://www.underhanded-c.org/_page_id_16.html

Re: The Three Ways of XOR

#53
post #33
post #17

"Sadly XOR doesn’t appear as an equivalent to NOT, AND and OR, as a logical operator on booleans, being relegated to just a bitewise operator in most programming languages." Well, in C there's just no need. The main raison d'etre for && and || over & and | is that you can exploit their short circuiting behaviour. A hypothetical ^^ operator wouldn't bring anything extra to the table.

The real problem is that you can not short circuit with XOR.

You have to evaluate both arguments to a XOR op to know its result. Short circuiting it has no natural meaning.

Re: The Three Ways of XOR

#54
post #49

Earlier quoted context omitted.

You have to take overflow into account. In most cases, that would make the algorithm not very useful compared to just doing the swap with a third variable.

Hm I don't think overflow will apply. Anything it does in an add will be undone by a subtract? Except that 2a, that might be trouble.

Imagine you only had 4 bit numbers (range 0-15) and you tried to do swap 14, 15 (1110, 1111). You can do that with xor but not with the add method, because you can't store a + b without a wider variable.

Re: The Three Ways of XOR

#55
post #45

Very early on in my computing career (nearly 40 years ago now), I remember being blown away when an older IBM Systems 360 programmer showed me how you can swap the values of two variables over WITHOUT using a third placeholder variable by using pure XOR. I didn't believe him until he showed me. Apparently they used to use it all the time to swap out entire segments of RAM in the S/360 without having to page out to di…

You can do the same with addition/subtract. Perhaps there is a simpler way, but here is one way to do it: a = a + b b = b + a a = b - a b = b - 2*a

a = a + b

b = a - b

a = a - b

I think this works too, assuming a+b doesn't overflow.

Re: The Three Ways of XOR

#56

Earlier quoted context omitted.

Hm I don't think overflow will apply. Anything it does in an add will be undone by a subtract? Except that 2a, that might be trouble.

Imagine you only had 4 bit numbers (range 0-15) and you tried to do swap 14, 15 (1110, 1111). You can do that with xor but not with the add method, because you can't store a + b without a wider variable.

15 + 15 gives 14

14 - 15 gives 15

Re: The Three Ways of XOR

#57
post #56

Earlier quoted context omitted.

Imagine you only had 4 bit numbers (range 0-15) and you tried to do swap 14, 15 (1110, 1111). You can do that with xor but not with the add method, because you can't store a + b without a wider variable.

15 + 15 gives 14 14 - 15 gives 15

The way I wrote this was rubbish, edited. I was trying to get at if you have 1110, 1111 then 1110 + 1111 will overflow, but you could xor them.

Re: The Three Ways of XOR

#58
post #45

Earlier quoted context omitted.

You can do the same with addition/subtract. Perhaps there is a simpler way, but here is one way to do it: a = a + b b = b + a a = b - a b = b - 2*a

a = a + b b = a - b a = a - b I think this works too, assuming a+b doesn't overflow.

Good one.

> I think this works too, assuming a+b doesn't overflow.

Well, in two's complement arithmetic (as is used on most architectures), the intermediate overflow can be ignored, and it will work just fine.

Re: The Three Ways of XOR

#59
post #49
post #45

Earlier quoted context omitted.

You can do the same with addition/subtract. Perhaps there is a simpler way, but here is one way to do it: a = a + b b = b + a a = b - a b = b - 2*a

You have to take overflow into account. In most cases, that would make the algorithm not very useful compared to just doing the swap with a third variable.

In two's complement arithmetic, you are basically computing modulo N (N = 2 to the power of the number of bits). So overflow will not interfere with the swap operation.

Re: The Three Ways of XOR

#60

Another way to look at XOR - it's an adder (the sum, without the carry)

Yeah, surprised this wasn't mentioned; XOR as addition mod 2 (note that they're just talking about XOR here, not bitwise XOR) comes up way more often than it being the most complex binary boolean operation.

[deleted]
Post reply on HN