C中的逻辑运算符有什么意义?

发布于 2024-08-31 13:02:11 字数 734 浏览 2 评论 0原文

我只是想知道 C 中是否有 XOR 逻辑运算符(类似于 && 用于 AND 但用于 XOR)。我知道我可以将 XOR 拆分为 AND、NOT 和 OR,但简单的 XOR 会更好。然后我突然想到,如果我在两个条件之间使用正常的异或按位运算符,它可能会起作用。对于我的测试来说确实如此。

考虑一下:

int i = 3;
int j = 7;
int k = 8;

仅仅为了这个相当愚蠢的例子,如果我需要 k 大于 i 或大于 j 但不能两者都大于, XOR 会非常方便。

if ((k > i) XOR (k > j))
   printf("Valid");
else
   printf("Invalid");

或者

printf("%s",((k > i) XOR (k > j)) ? "Valid" : "Invalid");

我进行按位异或 ^ 并产生“无效”。将两次比较的结果放入两个整数中会导致这 2 个整数包含 1,因此 XOR 产生错误。然后我尝试使用 &和 |按位运算符都给出了预期的结果。所有这些都是有意义的,因为知道真实条件具有非零值,而错误条件具有零值。

我想知道是否有理由使用逻辑 &&和 ||当按位运算符 &, | 时和 ^ 的作用是一样的吗?

I was just wondering if there is an XOR logical operator in C (something like && for AND but for XOR). I know I can split an XOR into ANDs, NOTs and ORs but a simple XOR would be much better. Then it occurred to me that if I use the normal XOR bitwise operator between two conditions, it might just work. And for my tests it did.

Consider:

int i = 3;
int j = 7;
int k = 8;

Just for the sake of this rather stupid example, if I need k to be either greater than i or greater than j but not both, XOR would be quite handy.

if ((k > i) XOR (k > j))
   printf("Valid");
else
   printf("Invalid");

or

printf("%s",((k > i) XOR (k > j)) ? "Valid" : "Invalid");

I put the bitwise XOR ^ and it produced "Invalid". Putting the results of the two comparisons in two integers resulted in the 2 integers to contain a 1, hence the XOR produced a false. I've then tried it with the & and | bitwise operators and both gave the expected results. All this makes sense knowing that true conditions have a non zero value, whilst false conditions have zero values.

I was wondering, is there a reason to use the logical && and || when the bitwise operators &, | and ^ work just the same?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烟雨扶苏 2024-09-07 13:02:11

你不需要逻辑异或,我已经忘记了SO问题,但它与你的想法类似,基本上我们不需要异或,它相当于 != 无论如何

FALSE XOR FALSE == FALSE
FALSE XOR TRUE == TRUE
TRUE XOR FALSE == TRUE
TRUE XOR TRUE == FALSE


FALSE != FALSE == FALSE
FALSE != TRUE == TRUE
TRUE != FALSE == TRUE
TRUE != TRUE == FALSE

我都会搜索我的最爱,稍后将链接粘贴到此处...

You don't need logical XOR, I have forgotten the SO question, but it's similar to what you're thinking, basically we don't need XOR, it's equivalent to != anyway

FALSE XOR FALSE == FALSE
FALSE XOR TRUE == TRUE
TRUE XOR FALSE == TRUE
TRUE XOR TRUE == FALSE


FALSE != FALSE == FALSE
FALSE != TRUE == TRUE
TRUE != FALSE == TRUE
TRUE != TRUE == FALSE

I'll search my favorites, and paste here the link later...

沉溺在你眼里的海 2024-09-07 13:02:11

按位运算符的工作方式与 && 不同。和 ||操作员。首先,&&和 ||执行短路计算,而按位运算符则不执行。换句话说,您不能使用按位运算符执行类似的操作:

int * p = 0;
(p != 0) && (*p = 1);

因为如果您说:

(p != 0) & (*p = 1);

两个子表达式都将被计算,并且您将取消引用空指针。

The bitwise operators do not work "just the same" as the && and || operator. For a start, && and || perform short-circuited evaluation, whereas the the bitwise operators do not. In other words, you can't do something like this with the bitwise operators:

int * p = 0;
(p != 0) && (*p = 1);

because if you said:

(p != 0) & (*p = 1);

both subexpressions would be evaluated, and you would dereference the null pointer.

生活了然无味 2024-09-07 13:02:11

当操作数为整数值时,按位 XOR 的工作方式与逻辑 XOR 不同:

2^4 ? "Valid" : "Invalid"

给出“有效”,但应给出“无效”

Bitwise XOR does not work as would a logical XOR when its operands are integer values:

2^4 ? "Valid" : "Invalid"

gives "Valid" but should give "Invalid"

缱绻入梦 2024-09-07 13:02:11

在 C 语言中,逻辑运算符的参数被视为布尔值 - 任何零都被视为“假”,而任何其他值(是的,负值也是)都被视为“真”。按位运算作用于各个位,正如尼尔已经指出的那样,它不像逻辑运算那样受到短路评估的影响。

在您的示例中,结果完全有效且符合预期,因为两个之间的按位异或为零。

In C arguments of logical operators are treated as Boolean values - anything zero is treated as "false", and anything else (yes, negative values too) are "true". Bitwise ops work on individual bits, and as Neil already noted, are not subject to short-circuit evaluation as logical ops are.

In your example the results are totally valid and expected since bit-wise xor between two ones is zero.

寄居人 2024-09-07 13:02:11

如果你想要 C 中的逻辑异或运算符,那么你可以使用这个:

#define xor != 0 ^ !!

它的工作原理是将表达式的两边都转换为布尔值并对它们进行异或。
您可以像使用 && 一样使用它。或||,像这样:

if (a xor b)

AFAICT,没有任何问题。

If you want a logical xor operator in C, then you can use this:

#define xor != 0 ^ !!

It works by converting both sides of the expression to booleans and xoring them.
You can use it as if you were using && or ||, like this:

if (a xor b)

AFAICT, there aren't any problems with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文