1

「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;
}

では、この場合、なぜ補数が必要なのでしょうか?

あなたの時間と配慮していただきありがとうございます。

4

1 に答える 1