相似按位运算符之间的差异 [英] difference between similar bitwise operators
问题描述
在涉及按位运算符时,!和有什么区别?和〜?我觉得他们俩都把这些都弄乱了,但是1可能会在最终答案中加1?
in refereing to bit-wise operators, what is the difference between ! and ~ ? I feel like they both flip the bits, but 1 maybe adds a 1 to the final answer?
~0xC4
compared to
!0xC4
谢谢!
推荐答案
!
不是按位运算符,它是布尔运算符.
!
is not a bitwise operator, it's a boolean operator.
布尔运算符对真实值(通常为int
)进行运算.任何非零值都为true,而0为false.结果始终为1
表示true,0
表示false.
The boolean operators operate on truth values, which are generally int
. Any non-zero value is true, while 0 is false. The result is always 1
for true, 0
for false.
-
!
不是布尔值 -
&&
是布尔值,并且 -
||
是布尔值或
!
is boolean not&&
is boolean and||
is boolean or
这些是例如if
,因为它需要一个布尔值.布尔和/或运算符也在短路,这意味着当结果已知时,它们将停止评估.很好,这意味着1 || crash_and_burn()
永远不会崩溃和燃烧.
These are the ones used in e.g. if
since it needs a boolean value. The boolean and/or operators are also short-circuiting, which means they stop evaluating when the result is known. This is good, it means 1 || crash_and_burn()
will never crash and burn.
但是,在提升等之后,按位运算符将对整数型参数的每个位进行运算.
But the bitwise operators operate on each bit of the integer-typed argument(s), after promotions and such of course.
-
~
不是按位的 -
&
是按位的,并且 -
|
是按位或 -
^
是按位或(xor)
~
is bitwise not&
is bitwise and|
is bitwise or^
is bitwise exlusive-or (xor)
按位运算符(当然)不是短路的,这没有任何意义,因为它们只是对位进行运算.请注意,虽然有一个^
按位运算符,但没有^^
布尔xor运算符.
The bitwise operators are (of course) not short-circuiting, that wouldn't make any sense since they just operate on pairs of bits. Note that while there is a ^
bitwise operator, there is no ^^
boolean xor operator.
这篇关于相似按位运算符之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!