简体   繁体   English

逻辑运算符

[英]Logical operators

I have come across an answer and I can't seem to figure out why it's right. 我遇到了一个答案,但似乎无法弄清楚为什么是正确的。

So there are two int variables x and y and they represent 0x66 and 0x39 respectively. 因此,有两个int变量x和y,它们分别表示0x66和0x39。

The question asked what is the result value based on the expression. 问题问基于表达式的结果值是多少。

x && y is apparently 0x01 (1)
x || y is 1
!x || !y is 0
x && ~y is 1

From what I was thinking, I thought as long as an argument was not zero it was considered true. 根据我的想法,我认为只要参数不为零,就可以认为是正确的。 So as long as x and y were some non-zero value then && operator would produce a 1, and of course as long as one of them is true the || 只要x和y为非零值,则&&运算符将生成1,当然,只要其中一个为真,||即可。 operator would produce a 1. 运算符将产生1。

So why is the third question 0? 那么为什么第三个问题为0? Is the ! 是个 ! different from the bitwise ~ operator? 不同于按位〜运算符? So originally x is 0101 0101 in binary since it's non zero it is true in the logical sense, but the ! 所以x最初是二进制的0101 0101,因为它不是零,从逻辑上讲是正确的,但是! of it would be false or would it do the one's complement of the number so its 1010 1010? 它是错误的,还是对数字进行补码,使其1010 1010?

A boolean result is always true or false, and in C true is represented by 1 and false by 0 . 布尔结果始终为true或false,在C中,true由1表示,false由0

The logical not operator ! 逻辑不是运算符! gives a boolean result, ie 1 or 0 . 给出布尔结果,即10 So if an expression is "true" (ie non-zero) then applying ! 因此,如果表达式为“ true”(即非零),则应用! on that expression will make it false, ie 0 . 该表达式上的值将使其为false,即0

In your example you have !x || !y 在您的示例中,您有!x || !y !x || !y . !x || !y First !x is evaluated, and it evaluates to false, leading to !y being evaluated, and it also evaluates to false, so the while expression becomes false, ie 0 . 首先对!x求值,并将其求值为false,导致对!y求值,并且还将其求值为false,因此while表达式变为false,即0

Yes, ! 是的! is different from the bitwise not ~ . 不同于按位不~ It's a logical not. 这是不合逻辑的。 It yields either 0 or 1. 它产生0或1。

~(0x01) will 0x10 and !(0x01) will 0x00 〜(0x01)将为0x10和!(0x01)将为0x00

'~' is a bitwise operator. “〜”是按位运算符。 '!' '!' is a logical operator. 是逻辑运算符。

From what I was thinking, I thought as long as an argument was not zero it was considered true. 根据我的想法,我认为只要参数不为零,就可以认为是正确的。 So as long as x and y were some non-zero value then && operator would produce a 1, and of course as long as one of them is true the || 只要x和y为非零值,则&&运算符将生成1,当然,只要其中一个为真,||即可。 operator would produce a 1. 运算符将产生1。

This is all correct. 这都是正确的。

So why is the third question 0? Is the ! different from the bitwise ~ operator?

Yes. 是。 It is logical NOT. 这是合乎逻辑的NOT。 Just like the other logical operators && and || 就像其他逻辑运算符&&|| , it only cares about if a variable has a non-zero value or not. ,它只关心变量是否具有非零值。

!0 will yield 1 . !0将产生1

!non_zero will yield 0 . !non_zero将产生0


Note that all logical operators in C yield type int rather than bool , as an ugly, backwards-compatibility remain from the time before C had a boolean type. 请注意,C语言中的所有逻辑运算符都产生int而不是bool类型,因为从C具有布尔类型之前一直存在一个丑陋的向后兼容性。 In C++ and other C-like languages, logical operators always yield type bool . 在C ++和其他类似C的语言中,逻辑运算符总是产生类型bool

Similarly, true and false in C are actually just macros that expand to the integers 1 and 0 . 同样,C语言中的truefalse实际上只是扩展为整数10宏。 In other languages they are keywords and boolean constants. 在其他语言中,它们是关键字和布尔常量。

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

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