An ordinary doubly linked list stores addresses of the previous and next list items in each list node, requiring two address fields: ... A B C D E ... –> next –> next –> next –> <– prev <– prev <– prev <– An XOR linked list compresses the same information into one address field by storing the bitwise XOR of the address for previous and the address for next in one field: ... A B C D E ... <–> A⊕C <-> B⊕D <-> C⊕E <-> More formally: link = addr⊕addr, link = addr⊕addr,... When traversing the list from left to right: supposing the cursor is at C, the previous item, B, may be XORed with the value in the link field. The address for D will then be obtained and list traversal may resume. The same pattern applies in the other direction. i.e. addr = link ⊕ addr where link = addr⊕addr so addr = addr⊕addr ⊕ addr
addr = addr⊕addr ⊕ addr since X⊕X = 0 => addr = 0 ⊕ addr since X⊕0 = x => addr = addr The XOR operation cancels addr appearing twice in the equation and all we are left with is the addr. To start traversing the list in either direction from some point, the address of two consecutive items is required. If the addresses of the two consecutive items are reversed, list traversal will occur in the opposite direction.
The key is the first operation, and the properties of XOR:
X⊕X = 0
X⊕0 = X
X⊕Y = Y⊕X
⊕Z = X⊕
The R2 register always contains the XOR of the address of current item C with the address of the predecessor item P: C⊕P. The Link fields in the records contain the XOR of the left and right successor addresses, say L⊕R. XOR of R2 with the current link field yields C⊕P⊕L⊕R.
If the predecessor was L, the P and L cancel outleaving C⊕R.
If the predecessor had been R, the P and R cancel, leaving C⊕L.
In each case, the result is the XOR of the current address with the next address. XOR of this with the current address in R1 leaves the next address. R2 is left with the requisite XOR pair of the current address and the predecessor.
Features
Two XOR operations suffice to do the traversal from one item to the next, the same instructions sufficing in both cases. Consider a list with items and with R1 and R2 being registers containing, respectively, the address of the current list item and a work register containing the XOR of the current address with the previous address. Cast as System/360 instructions:
X R2,Link R2 <- C⊕D ⊕ B⊕D XR R1,R2 R1 <- C ⊕ B⊕C
End of list is signified by imagining a list item at address zero placed adjacent to an end point, as in . The link field at A would be 0⊕B. An additional instruction is needed in the above sequence after the two XOR operations to detect a zero result in developing the address of the current item,
A list end point can be made reflective by making the link pointer be zero. A zero pointer is a mirror.
Drawbacks
General-purpose debugging tools cannot follow the XOR chain, making debugging more difficult;
The price for the decrease in memory usage is an increase in code complexity, making maintenance more expensive;
Not all languages support type conversion between pointers and integers, XOR on pointers is not defined in some contexts;
While traversing the list the address of the previously accessed node is needed to calculate the next node's address and the pointers will be unreadable if one isn't traversing the list—for example, if the pointer to a list item was contained in another data structure;
XOR linked lists do not provide some of the important advantages of doubly linked lists, such as the ability to delete a node from the list knowing only its address or the ability to insert a new node before or after an existing node when knowing only the address of the existing node.
Computer systems have increasingly cheap and plentiful memory, therefore storage overhead is not generally an overriding issue outside specialized embedded systems. Where it is still desirable to reduce the overhead of a linked list, unrolling provides a more practical approach.
Variations
The underlying principle of the XOR linked list can be applied to any reversible binary operation. Replacing XOR by addition or subtraction gives slightly different, but largely equivalent, formulations:
Addition linked list
... A B C D E ... <–> A+C <-> B+D <-> C+E <-> This kind of list has exactly the same properties as the XOR linked list, except that a zero link field is not a "mirror". The address of the next node in the list is given by subtracting the previous node's address from the current node's link field.
Subtraction linked list
... A B C D E ... <–> C-A <-> D-B <-> E-C <-> This kind of list differs from the standard "traditional" XOR linked list in that the instruction sequences needed to traverse the list forwards is different from the sequence needed to traverse the list in reverse. The address of the next node, going forwards, is given by adding the link field to the previous node's address; the address of the preceding node is given by subtracting the link field from the next node's address. The subtraction linked list is also special in that the entire list can be relocated in memory without needing any patching of pointer values, since adding a constant offset to each address in the list will not require any changes to the values stored in the link fields. This is an advantage over both XOR linked lists and traditional linked lists.