1

The Merge function of binomial queue in the book I read is shown below:

/*Merge two binomial queues*/
/*H1 contain the result*/
BinQueue Merge(BinQueue H1, BinQueue H2){
    BinTree T1, T2, Carry = NULL;
    int i, j;
    if (H1->CurrentSize + H2->CurrentSize > Capacity)
        Error("Merge would exceed capacity");
    H1->CurrentSize += H2->CurrentSize;
    for (i = 0, j = 1; j <= H1->CurrentSize; i++, j *= 2)
    {
        T1 = H1->TheTrees[i];
        T2 = H2->TheTrees[i];
        switch (!!T1 + 2 * !!T2 + 4 * !!Carry)
        {
        case 0:
        case 1:break;
        case 2:H1->TheTrees[i] = T2;
            H2->TheTrees[i] = NULL;
            break;
        case 4:H1->TheTree[i] = Carry;
            Carry = NULL;
            break;
        case 3:Carry = CombineTrees(T1, T2);
            H1->TheTrees[i] = H2->TheTrees[i] = NULL; break;
        case 5:Carry = CombineTrees(T1, Carry);
            H1->TheTrees[i] = NULL; break;
        case 6:Carry = CombineTrees(T2, Carry);
            H1->TheTrees[i] = NULL; break;
        case 7:
            H1->TheTree[i] = Carry;
            Carry = CombineTrees(T1, T2);
            H2->TheTrees[i] = NULL; break;
        }
    }
    return H1;
}

why does the author make logical negation twice to the pointer?

switch(!!T1+2*!!T2+4*!!Carry)

what's the different from doing nothing to the pointer?

Nishant Kumar
  • 2,199
  • 2
  • 22
  • 43
surrey
  • 53
  • 4

1 Answers1

4

That's a way to make a boolean value out of something that's not a boolean value. Remember that in C, zero is false while anything non-zero is true. That means to make a true boolean one or zero out of anything else you first use the logical not operator to create either a zero or a one, but this is now the opposite of whats wanted so we use the logical not operator again to get the actual boolean value.

Example: Lets say we have

int x = -56;

Now this is non-zero, and therefore "true" in the broader sense. To make a "proper" boolean zero or one we first use the logigcal not operator

!x

which gives you a 0 (false), but the value is actually true so we apply the operator again

!!x

to get the value 1.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621