0

Hi everyone I am sending to you my piece of code. I think to answer my question this code is enough so I didn't send all of code. In switch case I can see T1,T2 using with double ! what does that mean?

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 )//what does mean??
            {
                case 0: /* No trees */
                case 1: /* Only H1 */
                    break;
                case 2: /* Only H2 */
                    H1->TheTrees[ i ] = T2;
                    H2->TheTrees[ i ] = NULL;
                    break;
                case 4: /* Only Carry */
                    H1->TheTrees[ i ] = Carry;
                    Carry = NULL;
                    break;
                case 3: /* H1 and H2 */
                    Carry = CombineTrees( T1, T2 );
                    H1->TheTrees[ i ] = H2->TheTrees[ i ] = NULL;
                    break;
                case 5: /* H1 and Carry */
                    Carry = CombineTrees( T1, Carry );
                    H1->TheTrees[ i ] = NULL;
                    break;
                case 6: /* H2 and Carry */
                    Carry = CombineTrees( T2, Carry );
                    H2->TheTrees[ i ] = NULL;
                    break;
                case 7: /* All three */
                    H1->TheTrees[ i ] = Carry;
                    Carry = CombineTrees( T1, T2 );
                    H2->TheTrees[ i ] = NULL;
                    break;
            }
        }
        return H1;
    }
TryeR
  • 21

4 Answers4

5

! means logical NOT. !! means NOT NOT.

Logical NOT is 1 if the operand is zero, and 0 otherwise. !! is 1 if the operand is non-zero, and 0 if the operand is zero.

It's a convenient way to map the operand into binary (0, 1) scale.

You can rewrite !!p as (p ? 1 : 0).

In your case, !!p is zero if p is NULL, and 1 if p is non-NULL.

nullptr
  • 11,008
  • 1
  • 23
  • 18
3

! is logical NOT. !x is 1 if x is 0, or 0 if x is not 0.

!!x is 1 if !x is 0, or 0 if !x is not 0.

So !!x is 0 if x is 0, or 1 if x is not 0.

Short version: it converts all non-zero values to 1.

user253751
  • 57,427
  • 7
  • 48
  • 90
2

!! means not(not(integer)). A way to turn integers to boolean (true/false).

Example:

int a = 5;
int b = !!a; // b = 1
egur
  • 7,830
  • 2
  • 27
  • 47
1

! is an unary operator for "logical not", returning 1 (true) if the value is 0, or 0 (false) otherwise.

The same effect can be created using a comparison (a == 0) or the ternary operator (a ? 0 : 1).

By performing the operation twice, you'll get one more negation, so the final result for !!a would be (a == 0) == 0 or shorter: a != 0 (a ? 1 : 0).

The resulting code should (depending on your compiler) always be the same, but !a or !!a is just a lot shorter and easier to read, especially in longer expressions.

Used with pointers this essentially creates a simple check "is not a NULL pointer". For a simple if() this is not needed (due to implicit conversion to a boolean value), but you'll need it if you'd like to put the value/result into a bit field (as in this case).


Let's look at the switch statement:

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

What's happening here is essentially the creation of a bit field. The addition/multiplication is used to shift the bits and merge the result.

The bits from right to left:

  • !!T1: The first bit is set if T1 is not 0 (i.e. not NULL).
  • 2 * !!T2: The second bit is set if T2 is not 0.
  • 4 * !!Carry: The third bit is set if Carry is not 0.

The actual cases for the switch() then just represent these bit fields and are used for comparison to determine the proper code for the different cases.

Mario
  • 35,726
  • 5
  • 62
  • 78