What is the difference between bitwise and logical operators inside conditional statements in C?

こ雲淡風輕ζ 提交于 2019-12-02 10:06:02

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");

This prints:

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

This prints:

a
false
---
a
b
false

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

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.)

1101 & 0010 = 0000, 1101 && 0010 = True
1101 | 0010 = 1111, 1101 || 0010 = True

The reason is bitwise compares each bit separately whereas logical treats the whole bit string as one bit true or false. When looking at a bit string of one bit there is indeed no difference between logical and bitwise operators. Another way of thinking of it is bitwise operators are functions from integers to integers where logical operators are functions from booleans to booleans.

By calling table[0][i] ? "true" : "false" you are casting the integer into a bit. If you keep it as an integer you will see the difference between the two types of operators.

&& is a boolean operator, while & is a bitwise operator.

& is an and operation on two integers. Example: 1100 & 1001 = 1101, so 12 & 9 = 13.

&& only checks if both (left and right) values are TRUE (i.e. non-zero).

1 & 2 for example is 0, because a binary and of 1 and 2 is 0. Example: 01 & 10 = 00

While 1 && 2 is like TRUE && TRUE, which also equals true. So with &&, both left and right values are "converted" to a boolean expression first and are then compared.

Also, don't forget that compilers are capable of short circuiting && expressions. Like this one:

bool variable = isValid && compareSomething()

The right value is not evaluated, because it doesn't need to. The first one already states clearly that variable is isValid, as long as isValid is true.

Read more

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!