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

查看:114
本文介绍了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\n", 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天全站免登陆