以下代码中的逻辑运算符和位运算符之间是否存在任何功能差异?使用这一个或另一个的原因是什么?
typedef unsigned char BOOLEAN;
void orOperatorsComparison(BOOLEAN bBar, BOOLEAN bFoo)
{
BOOLEAN bLogicalOr = (bFoo || bBar);
BOOLEAN bBitwiseOr = (bFoo | bBar);
...
}
Run Code Online (Sandbox Code Playgroud) 网上有很多问题涉及按位和逻辑运算符之间的差异.希望我做了一个很好的搜索,当在条件语句中使用时,他们都没有专注于它们是否相同,也没有专门用于C语言.大多数人提到C++和C#,我不知道相同的答案是否也适用于C语言.
这是我编写的用于测试正在发生的事情的示例代码:
// 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] …Run Code Online (Sandbox Code Playgroud) c bitwise-operators logical-operators conditional-statements