简体   繁体   English

不应使用按位运算符代替逻辑运算符

[英]The bitwise operators should not be used in place of logical operators

//  How does this program work with ternary operator
#include <stdio.h>   
int main()
{
    int x = 2, y = 5;
    (x &  y) ? printf("True ") : printf("False "); // How do we get output
    (x && y) ? printf("True ") : printf("False ");
    return 0;
}

How does this program work? 该程序如何工作? How do the logical and bitwise operators work? 逻辑和按位运算符如何工作?

In this program & is a bitwise operator which will do bitwise and operation on 2 and 5. 在此程序中, &是按位运算符,它将按位进行运算, and在2和5上进行运算。

0000 0010<=2
0000 0101<=5
0000 0000<=output

So the first line will print False , whereas second one is normal and operator for which both values are true(non zero numbers ) so 2 && 5 will result in true and the output will be True . 因此,第一行将显示False ,而第二行将显示normal, and运算符的两个值均为true(非零数字),因此2 && 5将得出true,而输出将为True

The bitwise AND operator is a single ampersand: &. 按位AND运算符是单个&符:&。 A handy mnemonic is that the small version of the boolean AND, &&, works on smaller pieces (bits instead of bytes, chars, integers, etc). 一个方便的助记符是布尔AND和&&的小版本可用于较小的块(位而不是字节,字符,整数等)。 In essence, a binary AND simply takes the logical AND of the bits in each position of a number in binary form. 本质上,二进制“与”仅以二进制形式对数字的每个位置中的位进行逻辑“与”运算。

Example for & ( and ): &( )的示例:

00000010 & //2
00000101 = //5
--------
00000000   // 0

Bitwise OR works almost exactly the same way as bitwise AND. 按位或的工作原理与按位与的方法几乎完全相同。 The only difference is that only one of the two bits needs to be a 1 for that position's bit in the result to be 1. (If both bits are a 1, the result will also have a 1 in that position.) The symbol is a pipe: |. 唯一的区别是,对于该位置的位中的结果为1,只有两个位之一需要为1。(如果两个位均为1,则该位置的结果也将为1。)符号为管道:|。 Again, this is similar to boolean logical operator, which is ||. 同样,这类似于布尔逻辑运算符||。

Example for | 示例| ( OR ): ):

00000010 | //2
00000101 = //5
--------
00000111 //7

There is no boolean operator counterpart to bitwise exclusive-or, but there is a simple explanation. 没有按位异或的布尔运算符,但是有一个简单的解释。 The exclusive-or operation takes two inputs and returns a 1 if either one or the other of the inputs is a 1, but not if both are. 异或运算采用两个输入,并且如果一个输入或另一个输入均为1,则返回1,但如果两个输入都不为1,则返回1。 That is, if both inputs are 1 or both inputs are 0, it returns 0. Bitwise exclusive-or, with the operator of a caret, ^, performs the exclusive-or operation on each pair of bits. 即,如果两个输入均为1或两个输入均为0,则返回0。按插入号运算符^进行按位异或运算,对每对位执行异或运算。 Exclusive-or is commonly abbreviated XOR. 异或通常是XOR。

Example for ^ ( XOR ): ^( XOR )的示例:

00000010 ^ //2
00000101 = //5
--------
00000111 //7

If you get into a bitwise operators I suggest you get a pen and paper, imagine 2 random 8 bit numbers and work them all up on paper(all bitwise operations). 如果您遇到按位运算符,我建议您得到一支笔和纸,想象2个随机的8位数字并将它们全部处理在纸上(所有按位运算)。 Then you can find a Programmer calculator and check out your result. 然后,您可以找到一个程序员计算器,并查看结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM