I was looking over the source code for the "tfind" function from the "search.h" C library and I stumbled upon this line:
#define DEREFNODEPTR(NP) (node)((uintptr_t)(*(NP)) & ~((uintptr_t) 0x1))
This is how it's used:
/* Find datum in search tree.
KEY is the key to be located, ROOTP is the address of tree root,
COMPAR the ordering function. */
void *
__tfind (const void *key, void *const *vrootp, __compar_fn_t compar)
{
node root;
node *rootp = (node *) vrootp;
if (rootp == NULL)
return NULL;
root = DEREFNODEPTR(rootp);
CHECK_TREE (root);
while (DEREFNODEPTR(rootp) != NULL)
{
root = DEREFNODEPTR(rootp);
int r;
r = (*compar) (key, root->key);
if (r == 0)
return root;
rootp = r < 0 ? LEFTPTR(root) : RIGHTPTR(root);
}
return NULL;
}
So, why is one's complement needed in this case?
Thank you for your time and consideration.