我正在查看“search.h”C 库中“tfind”函数的源代码,我偶然发现了这一行:
#define DEREFNODEPTR(NP) (node)((uintptr_t)(*(NP)) & ~((uintptr_t) 0x1))
这是它的使用方式:
/* 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;
}
那么,为什么在这种情况下需要补码呢?
感谢您的时间和考虑。