C++ (x ^ 0x1) != 0 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20679642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:16:37  来源:igfitidea点击:

What does (x ^ 0x1) != 0 mean?

c++cbit-manipulationbitmask

提问by KodeWarrior

I came across the following code snippet

我遇到了以下代码片段

if( 0 != ( x ^ 0x1 ) )
     encode( x, m );

What does x ^ 0x1mean? Is this some standard technique?

什么x ^ 0x1意思?这是某种标准技术吗?

回答by Paul R

The XOR operation (x ^ 0x1) inverts bit 0. So the expression effectively means: if bit 0 of x is 0, or any other bit of x is 1, then the expression is true.

XOR 运算 ( x ^ 0x1) 反转位 0。因此该表达式有效地表示:如果 x 的位 0 为 0,或 x 的任何其他位为 1,则表达式为真。

Conversely the expression is false if x == 1.

相反,如果 x == 1,则表达式为假。

So the test is the same as:

所以测试是一样的:

if (x != 1)

and is therefore (arguably) unnecessarily obfuscated.

因此(可以说)被不必要地混淆了。

回答by Violet Giraffe

  • ^is the bitwise XORoperation
  • 0x1is 1in hex notation
  • x ^ 0x1will invert the last bit of x(refer to the XOR truth table in the link above if that's not clear to you).
  • ^是按位异或运算
  • 0x11十六进制表示法
  • x ^ 0x1将反转的最后一位x(如果您不清楚,请参阅上面链接中的 XOR 真值表)。

So, the condition (0 != ( x ^ 0x1 ))will be true if xis greater than 1 or if the last bit of xis 0. Which only leaves x==1 as a value at which the condition will be false. So it is equivalent to

因此,(0 != ( x ^ 0x1 ))如果x大于 1 或最后一位x为 0 ,则条件为真。这仅将 x==1 作为条件为假的值。所以它等价于

if (x != 1)

P. S. Hell of a way to implement such a simple condition, I might add. Don't do that. And if you must write complicated code, leave a comment. I beg of you.

PS 实现这样一个简单条件的方法太糟糕了,我可能会补充。不要那样做。如果您必须编写复杂的代码,请留下评论。我请求您。

回答by jwaliszko

This may seem as oversimplified explanation, but if someone would like to go through it slowly it is below:

这可能看起来过于简单化了,但如果有人想慢慢看,下面是:

^is a bitwise XORoperator in c, c++ and c#.

^是c、c++ 和 c# 中的按位异或运算符。

A bitwise XOR takes two bit patterns of equal length and performs the logical exclusive OR operation on each pair of corresponding bits.

Exclusive OR is a logical operation that outputs true whenever both inputs differ (one is true, the other is false).

按位异或采用两个等长的位模式,并对每对对应位执行逻辑异或运算。

异或是一种逻辑运算,只要两个输入不同(一个为真,另一个为假),就会输出真。

The truth tableof a xor b:

真值表一个XOR B

a           b        a xor b
----------------------------
1           1           0
1           0           1
0           1           1
0           0           0

So let's illustrate the 0 == ( x ^ 0x1 )expression on binary level:

因此,让我们0 == ( x ^ 0x1 )在二进制级别上说明表达式:

             what? xxxxxxxx (8 bits)
               xor 00000001 (hex 0x1 or 0x01, decimal 1)    
             gives 00000000
---------------------------
the only answer is 00000001

so:

所以:

   0 == ( x ^ 0x1 )    =>    x == 1
   0 != ( x ^ 0x1 )    =>    x != 1

回答by Vlad from Moscow

It is exclusive OR (XOR) operator. To understand how it works you can run this simple code

它是异或 (XOR) 运算符。要了解它是如何工作的,您可以运行这个简单的代码

    std::cout << "0x0 ^ 0x0 = " << ( 0x0 ^ 0x0 ) << std::endl;
    std::cout << "0x0 ^ 0x1 = " << ( 0x0 ^ 0x1 ) << std::endl;
    std::cout << "0x1 ^ 0x0 = " << ( 0x1 ^ 0x0 ) << std::endl;
    std::cout << "0x1 ^ 0x1 = " << ( 0x1 ^ 0x1 ) << std::endl;

The output will be

输出将是

0x0 ^ 0x0 = 0
0x0 ^ 0x1 = 1
0x1 ^ 0x0 = 1
0x1 ^ 0x1 = 0

So this expression

所以这个表情

0 != ( x ^ 0x1 )

will be equal true only when x != 0x1.

只有当 x != 0x1 时才等于真。

It does not change x itself. It only checks whether x is equal to 0 or 1. this rxpression could be changed to

它不会改变 x 本身。它只检查 x 是否等于 0 或 1。这个 rxpression 可以更改为

if ( x != 0x1 )

回答by Ferenc Deak

It checks that xis actually not 0x1... xoring xwith 0x1will result in 0 only if xis 0x1... this is an old trick mostly used in assembly language

它检查x实际上是不0x1...xor荷兰国际集团x0x1将导致0只要x0x1......这是大部分在汇编语言上使用的老把戏

回答by David Heffernan

The ^operator is bitwise xor. And 0x1is the number 1, written as a hexadecimal constant.

^操作是按位异或运算。和0x1是数字1,写成十六进制常数。

So, x ^ 0x1evaluates to a new value that is the same as x, but with the least significant bit flipped.

因此,x ^ 0x1计算为与 相同的新值x,但翻转了最低有效位。

The code does nothing more than compare x with 1, in a very convoluted and obscure fashion.

该代码只是以一种非常复杂和晦涩的方式将 x 与 1 进行比较。

回答by Paul

The xor (exclusive or) operator is most commonly used to invert one or more bits. The operation is to ask if excactly one of the bits are one, this leads to the following truth table (A and B are inputs, Y is output):

xor(异或)运算符最常用于反转一位或多位。操作是询问是否恰好有一位为 1,这导致以下真值表(A 和 B 是输入,Y 是输出):

A    B    Y
0    0    0
0    1    1
1    0    1
1    1    0

Now the purpose of this code seems to be to check if excatly the last bit is 1, and the others are 0, this equals if ( x != 1 ). The reason for this obscure method might be that prior bit manipulation techniques have been used and perhaps is used other places in the program.

现在这段代码的目的似乎是检查最后一位是否为 1,其他位是否为 0,这等于if ( x != 1 ). 这种晦涩的方法的原因可能是先前的位操作技术已经被使用,并且可能在程序的其他地方被使用。

回答by Chinna

^is bitwise xor operatorin c. In your case x is xor'ed with 1. for example xhas the value 10, then 10d ^ 1d ===> 1010b ^ 0001b = 1011b, 1011b == 11dso condition becomes true.

^是按位xor operatorc。在您的情况下,x 与 1 进行异或运算。例如x,值为 10,则10d ^ 1d ===> 1010b ^ 0001b = 1011b, 1011b == 11d条件变为真。

回答by undefined

The bitwise test seems to be a deliberate obfuscation, but if the underlying data is corporate data from an IBM mainframe system it may simply be that the code was written to reflect the original documentation. IBM data formats go back to the 1960's and frequently encode flags as single bits within a word to save storage. As the formats were modified, flag bytes were added at the end of the existing records to maintain backwards compatibility. The documentation for an SMF record, for example, might show the assembly language code to test three individual bits within three different words in a single record to decide that the data was an input file. I know much less about TCP/IP internals, but you may find bit flags there, as well.

按位测试似乎是故意混淆,但如果底层数据是来自 IBM 大型机系统的公司数据,则可能只是编写代码以反映原始文档。IBM 数据格式可以追溯到 1960 年代,并且经常将标志编码为一个字中的单个位以节省存储空间。随着格式的修改,在现有记录的末尾添加了标志字节以保持向后兼容性。例如,SMF 记录的文档可能会显示汇编语言代码以测试单个记录中三个不同字内的三个单独位,以确定数据是输入文件。我对 TCP/IP 内部结构知之甚少,但您也可以在那里找到位标志。

回答by ChuckCottrill

The operator ^ is the bitwise-xor (see &, | ). The result for a bit pair is,

运算符 ^ 是按位异或(参见 &, | )。位对的结果是,

0 ^ 0 == 0
0 ^ 1 == 1
1 ^ 0 == 1
1 ^ 1 == 0

So the expression,

所以表达式,

( x ^ 0x1 )

inverts/flips the 0th bit of x (leaving other bits unchanged).

反转/翻转 x 的第 0 位(保持其他位不变)。

Consider whether x can have values besides 0x0 and 0x1? When x is a single bit field, it can have only values 0x0 and 0x1, but when x is an int (char/short/long/etc), bits besides bit0 can affect the result of the expression.

考虑 x 是否可以有除 0x0 和 0x1 之外的值?当 x 是单个位字段时,它只能有 0x0 和 0x1 值,但是当 x 是 int (char/short/long/etc) 时,除 bit0 之外的位会影响表达式的结果。

The expression as given allows bits beside bit0 to affect the result,

给定的表达式允许位 0 旁边的位影响结果,

if ( 0 != ( x ^ 0x1 ) )

Which has equivalent truthiness as this (simpler) expression,

它与这个(更简单的)表达式具有等效的真实性,

if ( x ^ 0x1 )

Note that this expression would examine only bit0,

请注意,此表达式将仅检查位 0,

if( 0x1 & ( x ^ 0x1 ) )

So the expression as presented is really combining two expression checks,

所以所呈现的表达式实际上结合了两个表达式检查,

if( ( x & ~0x1 )  //look at all bits besides bit0
||  ( x ^ 0x1 ) ) //combine with the xor expression for bit0

Did the author intend to only check bit0, and have meant to use this expression,

作者是不是打算只检查bit0,打算用这个表达式,

if( 0x1 & ( x ^ 0x1 ) )

Or did the author intend to comingle the values for bit1-bitN and the xor of bit0?

还是作者打算混合 bit1-bitN 的值和 bit0 的异或?