C中条件语句中的位运算符和逻辑运算符有什么区别? [英] What is the difference between bitwise and logical operators inside conditional statements in C?

查看:23
本文介绍了C中条件语句中的位运算符和逻辑运算符有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网上有很多问题是指按位运算符和逻辑运算符之间的区别.希望我已经做了一个很好的搜索,它们都没有专门研究在条件语句中使用它们是否相同,也没有专门参考 C 语言.大多数人提到 C++ 和 C#,我不知道相同的答案是否也适用于 C 语言.

There are many questions on the net that refer to the differences between bitwise and logical operators. Hoping that I have done a good search, none of them specialize to whether they are the same or not when used inside conditional statements nor refer exclusively to C Language. The majority referred to C++ and C# and I do not know if the same answers were applicable to C Language too.

这是我为测试发生的情况而编写的示例代码:

This is an example code I wrote to test what is going on:

// Difference between logical && and bitwise & //

#include <stdio.h>

#define TRUE 123>45
#define FALSE 4>2342

void print_tt(int table[][4]);

int main(void) {

    int and_tt[2][4];   // AND truth table
    int or_tt[2][4];    // OR truth table

    // Create truth table for logical and bitwise AND operator all in one 2d array
    and_tt[0][0] = TRUE && TRUE ? 1 : 0;
    and_tt[0][1] = TRUE && FALSE ? 1 : 0;
    and_tt[0][2] = FALSE && TRUE ? 1 : 0;
    and_tt[0][3] = FALSE && FALSE ? 1 : 0;
    and_tt[1][0] = TRUE & TRUE ? 1 : 0;
    and_tt[1][1] = TRUE & FALSE ? 1 : 0;
    and_tt[1][2] = FALSE & TRUE ? 1 : 0;
    and_tt[1][3] = FALSE & FALSE ? 1 : 0;

    // Create truth table for logical and bitwise OR operator all in one 2d array
    or_tt[0][0] = TRUE || TRUE ? 1 : 0;
    or_tt[0][1] = TRUE || FALSE ? 1 : 0;
    or_tt[0][2] = FALSE || TRUE ? 1 : 0;
    or_tt[0][3] = FALSE || FALSE ? 1 : 0;
    or_tt[1][0] = TRUE | TRUE ? 1 : 0;
    or_tt[1][1] = TRUE | FALSE ? 1 : 0;
    or_tt[1][2] = FALSE | TRUE ? 1 : 0;
    or_tt[1][3] = FALSE | FALSE ? 1 : 0;

    puts("_______AND_______");
    puts("Logical   Bitwise");
    print_tt(and_tt);

    puts("_______OR________");
    puts("Logical   Bitwise");
    print_tt(or_tt);

}


// prints the truth table of the bitwise and logical operator given side by side
void print_tt(int table[][4]) {
    int i;
    for(i=0; i<4 ; ++i) {
        printf("%-10s%s
", table[0][i] ? "true" : "false",
            table[1][i] ? "true" : "false");
    }
}

程序的输出是:

_______AND_______
Logical   Bitwise
true      true
false     false
false     false
false     false
_______OR________
Logical   Bitwise
true      true
true      true
true      true
false     false

这证明了位运算符和逻辑运算符之间没有区别.修改TRUEFALSE宏的定义,把剩下的比较运算符也包括进去,可以看出又没有区别了.

Which proves that there are no differences between bitwise and logical operators. Changing the definition of TRUE and FALSE macros to include the remaining comparison operators, one can see that there is no difference again.

因此,如果存在差异,则可能与编译器解释语句的方式或代码的效率有关.

Therefore, if there are differences, these might be associated to the way the compiler interprets the statement or the efficiency of the code.

总之,在特定情况下,当我们在条件语句中的两个或多个比较操作的结果之间有一个按位或逻辑运算符时,我们应该使用两者中的哪一个,主要是为了提高效率?

In conclusion, in the specific case when we have a bitwise or logical operator between two or more results of a comparison operation inside a conditional statement, which of the two should we use, mostly for greater efficiency ?

推荐答案

您只检查值 01.尝试其他值,您会发现差异.

You're only checking the values 0 and 1. Try other values and you'll see differences.

int a = 4, b = 2;

puts(a && b ? "true" : "false");
puts(a & b  ? "true" : "false");

打印出来:

true
false

位运算符仅适用于整数.逻辑运算符可用于指针、浮点数和其他非整数类型.

Bitwise operators only work with integers. Logical operators can be used with pointers, floating point numbers, and other non-integral types.

还有短路.如果第一个操作数足够,逻辑运算符将不会计算第二个操作数.

There's also short-circuiting. The logical operators won't evaluate their second operand if the first was enough.

int a() { puts("a"); return 0; }
int b() { puts("b"); return 1; }

int main() {
    puts(a() && b() ? "true" : "false");
    puts("---");
    puts(a() & b()  ? "true" : "false");
}

打印出来:

a
false
---
a
b
false

注意使用 & 时如何打印 b.没有短路,所以 & 调用这两个函数,而 && 只调用 a().

Notice how b is printed when using &. There is no short-circuiting so & calls both functions, whereas && only calls a().

更微妙的一点是,与 && 不同,& 不会对其操作数施加求值顺序.输出同样可以将 ab 打印输出颠倒.

And on a subtler note, unlike &&, & does not impose an order of evaluation on its operands. The output could equally well have the a and b printouts reversed.

a
false
---
b
a
false

如果您将所有这些差异放在一边,那么是的,运算符是等价的.在这种情况下,不用担心效率.使用语义正确的运算符:逻辑运算符.

If you put all of these differences aside, then yes, the operators are equivalent. In that case, do not worry about efficiency. Use whichever operators are semantically correct: the logical ones.

(如果它可以帮助您放松心情,那么效率不会有任何差异.编译器非常聪明,并且肯定会发出最佳字节码来评估这些表达式,无论您使用哪个运算符.)

(If it helps ease your mind, there will be no difference in efficiency. Compilers are very smart and will certainly emit the optimal bytecode to evaluate these expressions, whichever operator you use.)

这篇关于C中条件语句中的位运算符和逻辑运算符有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆