Search code examples
clinked-listxor

XOR Linked List XOR Function


I came across this piece of code several times while looking at the implementation of XOR Linked Lists , but none of them seemed to explain this line properly (or maybe I missed out something) -

struct node* XOR (struct node *a, struct node *b)
{
    return (struct node*) ((unsigned int) (a) ^ (unsigned int) (b));
}

How does it work ? Can anyone please explain the casts involved too ?(please point out any previous answers/comments which have described it) Thanks !


Solution

  • Apart from the usual XOR performed on the addresses pointed by the 'a' and the 'b' pointers

    (unsigned int) (a) ^ (unsigned int) (b)
    

    it is the implicit conversion of int to pointer (struct node * here) that makes this code work.

    (struct node *)(unsigned int someInteger); 
    

    EDIT : Thanks to @aruisdante for explaining the second part !