HACKER Q&A
📣 ummonk

Is it possible to swap two signed integers in C without a third variable


The standard tricks of using arithmetic or xor don't work because both have potentially undefined behavior on some values of signed integers.

If sizeof(int) were guaranteed to be sizeof(unsigned int) then maybe a cast could do it: (unsigned int )&a = (unsigned int )&a ^ (unsigned int )&b; (unsigned int )&b = (unsigned int )&a ^ (unsigned int )&b; (unsigned int )&a = (unsigned int )&a ^ (unsigned int )&b;

What would be a general solution? Is there a way to use the preprocessor to spit out sizeof(int) lines that do bitwise xor on every respective byte in a and b?


  👤 retrocryptid Accepted Answer ✓
I looked at the C99 and C11 specs and sure enough, there's nothing that says sizeof( int ) == sizeof( unsigned int ). But I would think it would be common enough you could assume it's true. I mean, have you ever seen a machine made after 1970 where sizeof( int ) and sizeof( unsigned int ) were different? (or short / unsigned short, long / unsigned long, long long / unsigned long long). I suspect this is a no-go since you went to the trouble of posting this question.

I'm sure you could do it byte by byte and hope the compiler could optimize it.

(I don't have the C17 spec around, so maybe someone else could check it didn't add that constraint, in which case my response would be "Of course! just use C17!")


👤 efas
Implicitly, the two signed integers are large enough to fit each other (they are the same size). I would not assume anything about organization in memory though. x=a, y=b; look at arithmetic in pseudo-ops x=a, y=(b-a); there will be an overflow here or after the negation step x=a+(b-a)=b, y=(b-a); x=b, y=(a-b); negate the unitary value could overflow (and set the carry bit) x=b, y=(a-b)+b=a; oops, four operations and not as elegant as XORs can be. Or, the compiler would probably use a third register.

👤 fwsgonzo
It's not a trick to use XOR to swap variables. XOR is a code pessimization. You should be using std::swap or just use a temporary which generates proper machine code. Most instruction sets have exchange which is 1 instruction.

I guess that answers your question. You can swap two integers on x86. See the xchg instruction.

https://www.felixcloutier.com/x86/xchg