# Why use logic gates in this situation? (XOR gates)

I came across this circuit that swaps logical signals:

However I don't understand why would you need to use logic gates. Would it be possible to just connect the A on the left to the B on the right and the same with the B on the left (maybe add a diode to prevent current from going back?).

And one last question; would an "Enable" signal make this circuit more useful (only swaping when Enable is high)?

Maybe I'm missing something. Thank you.

• I also think you don't need to use such complex network for a swap. I'd expect this network to be shown as an exercise, an example. Where did you find it? Did the source claim that it is "useful" in some practical way? Textbooks often propose exercises/examples whose only aim is to make the reader perform mental gymnastic, and which are useless in practice. They are proposed since the mental gym they cause is useful to learn or consolidate the theoretical aspects, which can then be applied to more realistic cases.
– chi
Aug 12, 2018 at 18:15
• Yeah I think it is kind of an exercise. It was in wikipedia page of the XOR gate but in the spanish version only link. It just mentions it and says it can be archieved with 8 NAND gates and that it "swaps". > Did the source claim that it is "useful" in some practical way? No, it just mentions it. That's why I was curious if it was usefull or not. Edit: It also mentions it allows the swap of signals "without the need of a conection between layers on a circuit"
– Juan
Aug 12, 2018 at 21:14
• I think the last part refers that you don't need to "cross the wires" using the logic above, unlike the "trivial" solution.
– chi
Aug 12, 2018 at 21:44

One reason for using such a circuit is to avoid wire crossing. Integrated circuits are laid out on several planar layers, and inside each layer, wires are not allowed to cross (for obvious reasons). Hence if you want to switch two wires, you either need to reach out to a different layer, or to use a gadget such as the one shown. I have no idea whether this kind of gadget is actually used in practice.

It should be noted that same idea can be used to swap two values without using any additional memory:

1. $A \gets A \oplus B$
2. $B \gets B \oplus A$
3. $A \gets A \oplus B$

In a C-like language, this would be A ^= B; B ^= A; A ^= B. This code corresponds to an execution of your circuit in topological order.

• Thank you very much for your answer. This might be very nitpicky but wouldn't a 0 ohm resistor do the job (and a diode to prevent current from going back maybe)? (they use them for this purposes). I think this circuit is only for practice/curiosity. On a last note, you are right on that this allows to swap without additional memory and that may be usefull.
– Juan
Aug 12, 2018 at 22:28
• I'm not an expert on electronics, but I'm not sure you can realize your suggested alternative circuit on every kind of IC. Aug 12, 2018 at 22:31
• Exercise: Implement an XOR gate in CMOS (or NMOS) without any wire crossings. Aug 13, 2018 at 1:51
• But is it possible to even implement a xor gate without crossing wires? Aug 13, 2018 at 12:32