Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
操作员有什么用!!在C中?_C_Pointers_Operators - Fatal编程技术网

操作员有什么用!!在C中?

操作员有什么用!!在C中?,c,pointers,operators,C,Pointers,Operators,大家好,我把我的代码发给你们。 我想回答我的问题,这个代码足够了,所以我没有发送所有的代码。 在开关的情况下,我可以看到T1,T2使用双!这是什么意思 BinQueue Merge( BinQueue H1, BinQueue H2 ) { BinTree T1, T2, Carry = NULL; int i, j; if( H1->CurrentSize + H2->CurrentSize > Capacit

大家好,我把我的代码发给你们。 我想回答我的问题,这个代码足够了,所以我没有发送所有的代码。 在开关的情况下,我可以看到T1,T2使用双!这是什么意思

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

!!表示非整数。将整数转换为布尔真/假的方法

例如:

int a = 5;
int b = !!a; // b = 1
!!表示不符合逻辑!!意思是不

如果操作数为零,则逻辑NOT为1,否则为0。 !! 如果操作数为非零,则为1;如果操作数为零,则为0

这是一种将操作数映射为二进制0,1比例的方便方法

你可以重写!!p作为p?1:0

在你的情况下!!如果p为空,则p为零;如果p为非空,则p为1。

!这是不合逻辑的!如果x为0,则x为1;如果x不是0,则x为0

!!x是1如果!x是0,如果是,则为0!x不是0

所以!!如果x为0,则x为0;如果x不是0,则x为1

短版本:它将所有非零值转换为1。

!是逻辑not的一元运算符,如果值为0,则返回1 true,否则返回0 false

使用比较a==0或三元运算符a?可以创建相同的效果?0:1

通过执行两次操作,您将获得一个否定,因此,的最终结果为!!a应该是a==0==0或更短:a!=0 a?1:0

根据编译器的不同,生成的代码应该始终相同,但是!a或!!a只是要短得多,更容易阅读,尤其是在较长的表达中

与指针一起使用,这实质上创建了一个简单的检查,该检查不是空指针。对于一个简单的例子,如果由于隐式转换为布尔值而不需要它,但是如果您想像本例中那样将值/结果放入位字段,则需要它

让我们看看switch语句:

switch( !!T1 + 2 * !!T2 + 4 * !!Carry )
这里发生的事情本质上是创建一个位字段。加法/乘法用于移位位和合并结果

从右到左的位:

!!T1:如果T1不为0,即不为空,则设置第一位。 2 * !!T2:如果T2不是0,则设置第二位。 4 * !!进位:如果进位不是0,则设置第三位。

实际的交换机实例只表示这些位字段,用于比较,以确定不同情况下的正确代码。< /P>将问题提得更好一点,这是C,而不是C++,因此它只将整数变成0或1。