I came across the following code snippet
我遇到了下面的代码片段。
if( 0 != ( x ^ 0x1 ) )
encode( x, m );
What does x ^ 0x1
mean? Is this some standard technique?
x ^ 0 x1是什么意思?这是标准的技术吗?
17 个解决方案
#1
278
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.
异或操作(x ^ 0 x1)反转位0。这个表达式的意思是:如果x = 0,或者其他x = 1,那么表达式为真。
Conversely the expression is false if x == 1.
反之,如果x = 1,则表达式为false。
So the test is the same as:
所以测试是一样的:
if (x != 1)
and is therefore (arguably) unnecessarily obfuscated.
因此(可以说)是不必要的混淆。
#2
77
-
^
is the bitwise XOR operation - ^按位异或操作
-
0x1
is1
in hex notation - 0x1在十六进制表示法中是1。
-
x ^ 0x1
will invert the last bit ofx
(refer to the XOR truth table in the link above if that's not clear to you). - x ^ 0 x1将反转x的最后一点(请参阅上面的XOR真值表的链接如果你还不清楚)。
So, the condition (0 != ( x ^ 0x1 ))
will be true if x
is greater than 1 or if the last bit of x
is 0. Which only leaves x==1 as a value at which the condition will be false. So it is equivalent to
所以,条件(0 ! =(x ^ 0 x1))将是真的如果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.
我可能会说,实现这样一个简单的条件的方法是,不要那样做。如果您必须编写复杂的代码,请留下评论。我请求你。
#3
48
This may seem as oversimplified explanation, but if someone would like to go through it slowly it is below:
这似乎是过度简化的解释,但如果有人愿意慢慢地解释,它就在下面:
^
is a bitwise XOR operator 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.
一个位的XOR取两个相同长度的位模式,并在每对对应的位上执行逻辑上的独占或操作。
Exclusive OR is a logical operation that outputs true whenever both inputs differ (one is true, the other is false).
当两个输入都不同的时候(一个为真,另一个为假),独占性或逻辑运算输出为真。
The truth table of 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
#4
33
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 )
#5
18
It checks that x
is actually not 0x1
... xor
ing x
with 0x1
will result in 0 only if x
is 0x1
... this is an old trick mostly used in assembly language
它检查x是不是0x1…xoring x与0x1只会在x = 0x1时得到0…这是大多数汇编语言使用的老把戏。
#6
17
The ^
operator is bitwise xor. And 0x1
is the number 1
, written as a hexadecimal constant.
算子是位xor。0x1是1,写成16进制常数。
So, x ^ 0x1
evaluates to a new value that is the same as x
, but with the least significant bit flipped.
所以,x ^ 0 x1评估一个新值,等于x,但最低有效位翻转。
The code does nothing more than compare x with 1, in a very convoluted and obscure fashion.
这段代码所做的只是将x与1进行比较,这是一种非常复杂和晦涩的方式。
#7
10
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.
现在,这个代码的目的似乎是要检查是否excatly是1,而其他的是0,这等于if (x != 1)。这种模糊方法的原因可能是之前的位操作技术已经被使用,或者在程序中使用了其他位置。
#8
7
^
is bitwise xor operator
in c
. In your case x is xor'ed with 1. for example x
has the value 10, then 10d ^ 1d ===> 1010b ^ 0001b = 1011b, 1011b == 11d
so condition becomes true.
^按位异或运算符在c。在你的情况中x是“异或”1。例如,x有值10,然后10d 1d ==> 1010b 0001b = 1011b, 1011b == 11d,条件变为真。
#9
7
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.
bitwise测试似乎是故意的混淆,但是如果底层数据是来自IBM大型机系统的企业数据,那么它可能只是为了反映原始文档而编写的代码。IBM的数据格式可以追溯到20世纪60年代,并且经常将标记编码为单个字节,以节省存储空间。随着格式的修改,在现有记录的末尾添加了标记字节以保持向后兼容性。例如,SMF记录的文档可能会显示汇编语言代码,以在单个记录中测试三个不同单词中的三个单独的位,从而决定数据是一个输入文件。我对TCP/IP内部的了解少了很多,但是您也可以在那里找到一些标记。
#10
6
The operator ^ is the bitwise-xor (see &, | ). The result for a bit pair is,
操作员^是位xor(见| &)。一对位对的结果是,
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).
反转/翻转第0位x(保持其他位不变)。
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是一个整数(char/short/long/等)时,比特位之外的位元就会影响表达式的结果。
The expression as given allows bits beside bit0 to affect the result,
给定的表达式允许bit0旁边的比特来影响结果,
if ( 0 != ( x ^ 0x1 ) )
Which has equivalent truthiness as this (simpler) expression,
这就像这个(更简单)的表达式一样,
if ( x ^ 0x1 )
Note that this expression would examine only bit0,
注意,这个表达式只检查bit0,
if( 0x1 & ( x ^ 0x1 ) )
So the expression as presented is really combining two expression checks,
因此,present的表达式实际上是结合了两个表达式检查,
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?
或者,作者是否打算将比特- bitn和xor的值合并到一起?
#11
6
I'm adding a new answer because no one really explained how to get the answer intuitively.
我添加了一个新的答案,因为没有人真正解释如何直观地得到答案。
The inverse of +
is -
.
The inverse of ^
is ^
.
+的倒数是-。的逆^ ^。
How do you solve 0 != x - 1
for x
? You + 1
to both sides: 0 + 1 != x - 1 + 1
→ 1 != x
.
How do you solve 0 != x ^ 1
for x
? You ^ 1
to both sides: 0 ^ 1 != x ^ 1 ^ 1
→ 1 != x
.
如何解x = x - 1 ?两边都+ 1:0 + 1 ! = x - 1 + 1→1 ! = x,你如何解决0 ! x = x ^ 1 ?你对双方都有:0 1 != x 1 1 != x。
#12
6
I'd guess that there are other bits or bit-field values in x
, and this is intended to test that only the low-order bit is set. In the context, I'd guess that this is the default, and that therefore encoding of this and some related m
(probably more expensive to encode) can be skipped, because they must both be the default value, initialized in a constructor or similar.
我猜想有其他位或位域值x,这是为了测试,只有在上下文中,已经设置了低阶位。我猜这是默认,因此编码的和一些相关的m(可能更昂贵的编码)可以跳过,因为他们必须都是默认值,初始化构造函数或类似。
Somehow the decoder must be able to infer that these values are missing. If they are at the end of some structure, it may be communicated via a length
value that's always present.
某种程度上,解码器必须能够推断出这些值丢失了。如果它们处于某种结构的末端,它可以通过始终存在的长度值进行通信。
#13
4
The XOR is useful in C# flag enum. To remove single flag from enum value it is necessary to use xor operator (reference here)
XOR在c# flag enum中很有用。为了从枚举值中移除单个标记,需要使用xor运算符(此处引用)
Example:
例子:
[Flags]
enum FlagTest { None 0x0, Test1 0x1, Test2 0x2, Test3 0x4}
FlagTest test = FlagTest.Test2 | FlagTest.Test3;
Console.WriteLine(test); //Out: FlagTest.Test2 | FlagTest.Test3
test = test ^ FlagTest.Test2;
Console.WriteLine(test); //Out: FlagTest.Test3
#14
3
There are a lot of good answers but I like to think of it in a simpler way.
有很多不错的答案,但我喜欢用更简单的方法来思考。
if ( 0 != ( x ^ 0x1 ) );
First of all. An if statement is only false if the argument is zero. This means that comparing not equal to zero is pointless.
首先。如果参数为零,则if语句为假。这意味着比较不等于零是没有意义的。
if ( a != 0 );
// Same as
if ( a );
So that leaves us with:
这就留给我们:
if ( x ^ 0x1 );
An XOR with one. What an XOR does is essentially detect bits that are different. So, if all the bits are the same it will return 0. Since 0 is false, the only time it will return false is if all of the bits are the same. So it will be false if the arguments are the same, true if they are different...just like the not equal to operator.
一个一个的异或。XOR所做的就是检测不同的比特。所以,如果所有的位都相同,它会返回0。因为0是假的,所以唯一返回false的时间是如果所有的比特都是相同的。所以如果论点是一样的,那是错误的,如果它们是不同的…就像不等于运算符一样。
if ( x != 0x1 );
If fact, the only difference between the two is that !=
will return 0 or 1, while ^
will return any number, but the truthyness of the result will always be the same. An easy way to think about it is.
事实上,两者之间唯一的区别是,! =将返回0或1,^将返回任意数量,但结果的truthyness永远是相同的。一种简单的思考方式是。
(b != c) === !!(b ^ c) // for all b and c
The final "simplification" is converting 0x1
to decimal which is 1. Therefore your statement is equivalent to:
最后的“简化”是将0x1转换成十进制,也就是1。因此,你的声明相当于:
if ( x != 1 )
#15
0
^ is a bitwise XOR operator
^按位异或运算符
If x = 1
如果x = 1
00000001 (x) (decimal 1)
00000001 (0x1) (decimal 1)
XOR 00000000 (0x0) (decimal 0)
here 0 == ( x ^ 0x1 )
0 = =(x ^ 0 x1)
If x = 0
如果x = 0
00000000 (x) (decimal 0)
00000001 (0x1) (decimal 1)
XOR 00000001 (0x1) (decimal 0)
here 0 != ( x ^ 0x1 )
这里0 ! =(x ^ 0 x1)
The truth table of a xor b:
xor b的真值表:
a b a xor b
----------------------------
1 1 0
1 0 1
0 1 1
0 0 0
The code simply means
代码仅仅意味着
#16
0
As I see the answers so far miss a simple rule for handling XOR
s. Without going into details what ^
and 0x
mean (and if
, and !=
etc), the expression 0 != (x^1)
can be reworked as follows using the fact that (a^a)==0
:
在我看来,到目前为止的答案中没有一个简单的规则来处理XORs。不详细说明什么是和0x是什么意思(如果,and !=等等),表达式0 != (x 1)可以用(a a)==0的事实改写如下:
0 != (x^1) <=> [xor left and right side by 1]
(0^1) != (x^1^1) <=>
1 != x
#17
0
The standard technique that might be being used, here, is to repeat an idiom as it appears in surrounding context for clarity, rather than to obfuscate it by replacing it with an idiom that is arithmetically simpler but contextually meaningless.
这里使用的标准技术是重复一个习语,因为它出现在周围的环境中,为了清晰,而不是用一个简单的、但上下文无关的成语来代替它。
The surrounding code may make frequent reference to (x ^ 1)
, or the test may be asking "if bit 0 was the other way around, would this bit-mask be empty?".
周围的代码可能经常引用(x ^ 1),或测试可能会问“如果0是反过来,这个位元遮罩是空的吗?”。
Given that the condition causes something to be encode()
ed, it may be that in context the default state of bit 0 has been inverted by other factors, and we need only encode extra information if any of the bits deviate from their default (normally all-zero).
考虑到条件导致某些东西被编码()ed,可能在上下文环境中,比特0的默认状态被其他因素颠倒了,我们只需要编码额外的信息,如果有任何位偏离了它们的默认值(通常为零)。
If you take the expression out of context and ask what it does, you overlook the underlying intention. You might just as well look at the assembly output from the compiler and see that it simply does a direct equality comparison with 1.
如果你把这个表达从语境中去掉,然后问它做了什么,你就会忽略潜在的意图。您也可以从编译器中查看程序集的输出,并看到它只与1进行直接的相等比较。
#1
278
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.
异或操作(x ^ 0 x1)反转位0。这个表达式的意思是:如果x = 0,或者其他x = 1,那么表达式为真。
Conversely the expression is false if x == 1.
反之,如果x = 1,则表达式为false。
So the test is the same as:
所以测试是一样的:
if (x != 1)
and is therefore (arguably) unnecessarily obfuscated.
因此(可以说)是不必要的混淆。
#2
77
-
^
is the bitwise XOR operation - ^按位异或操作
-
0x1
is1
in hex notation - 0x1在十六进制表示法中是1。
-
x ^ 0x1
will invert the last bit ofx
(refer to the XOR truth table in the link above if that's not clear to you). - x ^ 0 x1将反转x的最后一点(请参阅上面的XOR真值表的链接如果你还不清楚)。
So, the condition (0 != ( x ^ 0x1 ))
will be true if x
is greater than 1 or if the last bit of x
is 0. Which only leaves x==1 as a value at which the condition will be false. So it is equivalent to
所以,条件(0 ! =(x ^ 0 x1))将是真的如果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.
我可能会说,实现这样一个简单的条件的方法是,不要那样做。如果您必须编写复杂的代码,请留下评论。我请求你。
#3
48
This may seem as oversimplified explanation, but if someone would like to go through it slowly it is below:
这似乎是过度简化的解释,但如果有人愿意慢慢地解释,它就在下面:
^
is a bitwise XOR operator 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.
一个位的XOR取两个相同长度的位模式,并在每对对应的位上执行逻辑上的独占或操作。
Exclusive OR is a logical operation that outputs true whenever both inputs differ (one is true, the other is false).
当两个输入都不同的时候(一个为真,另一个为假),独占性或逻辑运算输出为真。
The truth table of 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
#4
33
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 )
#5
18
It checks that x
is actually not 0x1
... xor
ing x
with 0x1
will result in 0 only if x
is 0x1
... this is an old trick mostly used in assembly language
它检查x是不是0x1…xoring x与0x1只会在x = 0x1时得到0…这是大多数汇编语言使用的老把戏。
#6
17
The ^
operator is bitwise xor. And 0x1
is the number 1
, written as a hexadecimal constant.
算子是位xor。0x1是1,写成16进制常数。
So, x ^ 0x1
evaluates to a new value that is the same as x
, but with the least significant bit flipped.
所以,x ^ 0 x1评估一个新值,等于x,但最低有效位翻转。
The code does nothing more than compare x with 1, in a very convoluted and obscure fashion.
这段代码所做的只是将x与1进行比较,这是一种非常复杂和晦涩的方式。
#7
10
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.
现在,这个代码的目的似乎是要检查是否excatly是1,而其他的是0,这等于if (x != 1)。这种模糊方法的原因可能是之前的位操作技术已经被使用,或者在程序中使用了其他位置。
#8
7
^
is bitwise xor operator
in c
. In your case x is xor'ed with 1. for example x
has the value 10, then 10d ^ 1d ===> 1010b ^ 0001b = 1011b, 1011b == 11d
so condition becomes true.
^按位异或运算符在c。在你的情况中x是“异或”1。例如,x有值10,然后10d 1d ==> 1010b 0001b = 1011b, 1011b == 11d,条件变为真。
#9
7
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.
bitwise测试似乎是故意的混淆,但是如果底层数据是来自IBM大型机系统的企业数据,那么它可能只是为了反映原始文档而编写的代码。IBM的数据格式可以追溯到20世纪60年代,并且经常将标记编码为单个字节,以节省存储空间。随着格式的修改,在现有记录的末尾添加了标记字节以保持向后兼容性。例如,SMF记录的文档可能会显示汇编语言代码,以在单个记录中测试三个不同单词中的三个单独的位,从而决定数据是一个输入文件。我对TCP/IP内部的了解少了很多,但是您也可以在那里找到一些标记。
#10
6
The operator ^ is the bitwise-xor (see &, | ). The result for a bit pair is,
操作员^是位xor(见| &)。一对位对的结果是,
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).
反转/翻转第0位x(保持其他位不变)。
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是一个整数(char/short/long/等)时,比特位之外的位元就会影响表达式的结果。
The expression as given allows bits beside bit0 to affect the result,
给定的表达式允许bit0旁边的比特来影响结果,
if ( 0 != ( x ^ 0x1 ) )
Which has equivalent truthiness as this (simpler) expression,
这就像这个(更简单)的表达式一样,
if ( x ^ 0x1 )
Note that this expression would examine only bit0,
注意,这个表达式只检查bit0,
if( 0x1 & ( x ^ 0x1 ) )
So the expression as presented is really combining two expression checks,
因此,present的表达式实际上是结合了两个表达式检查,
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?
或者,作者是否打算将比特- bitn和xor的值合并到一起?
#11
6
I'm adding a new answer because no one really explained how to get the answer intuitively.
我添加了一个新的答案,因为没有人真正解释如何直观地得到答案。
The inverse of +
is -
.
The inverse of ^
is ^
.
+的倒数是-。的逆^ ^。
How do you solve 0 != x - 1
for x
? You + 1
to both sides: 0 + 1 != x - 1 + 1
→ 1 != x
.
How do you solve 0 != x ^ 1
for x
? You ^ 1
to both sides: 0 ^ 1 != x ^ 1 ^ 1
→ 1 != x
.
如何解x = x - 1 ?两边都+ 1:0 + 1 ! = x - 1 + 1→1 ! = x,你如何解决0 ! x = x ^ 1 ?你对双方都有:0 1 != x 1 1 != x。
#12
6
I'd guess that there are other bits or bit-field values in x
, and this is intended to test that only the low-order bit is set. In the context, I'd guess that this is the default, and that therefore encoding of this and some related m
(probably more expensive to encode) can be skipped, because they must both be the default value, initialized in a constructor or similar.
我猜想有其他位或位域值x,这是为了测试,只有在上下文中,已经设置了低阶位。我猜这是默认,因此编码的和一些相关的m(可能更昂贵的编码)可以跳过,因为他们必须都是默认值,初始化构造函数或类似。
Somehow the decoder must be able to infer that these values are missing. If they are at the end of some structure, it may be communicated via a length
value that's always present.
某种程度上,解码器必须能够推断出这些值丢失了。如果它们处于某种结构的末端,它可以通过始终存在的长度值进行通信。
#13
4
The XOR is useful in C# flag enum. To remove single flag from enum value it is necessary to use xor operator (reference here)
XOR在c# flag enum中很有用。为了从枚举值中移除单个标记,需要使用xor运算符(此处引用)
Example:
例子:
[Flags]
enum FlagTest { None 0x0, Test1 0x1, Test2 0x2, Test3 0x4}
FlagTest test = FlagTest.Test2 | FlagTest.Test3;
Console.WriteLine(test); //Out: FlagTest.Test2 | FlagTest.Test3
test = test ^ FlagTest.Test2;
Console.WriteLine(test); //Out: FlagTest.Test3
#14
3
There are a lot of good answers but I like to think of it in a simpler way.
有很多不错的答案,但我喜欢用更简单的方法来思考。
if ( 0 != ( x ^ 0x1 ) );
First of all. An if statement is only false if the argument is zero. This means that comparing not equal to zero is pointless.
首先。如果参数为零,则if语句为假。这意味着比较不等于零是没有意义的。
if ( a != 0 );
// Same as
if ( a );
So that leaves us with:
这就留给我们:
if ( x ^ 0x1 );
An XOR with one. What an XOR does is essentially detect bits that are different. So, if all the bits are the same it will return 0. Since 0 is false, the only time it will return false is if all of the bits are the same. So it will be false if the arguments are the same, true if they are different...just like the not equal to operator.
一个一个的异或。XOR所做的就是检测不同的比特。所以,如果所有的位都相同,它会返回0。因为0是假的,所以唯一返回false的时间是如果所有的比特都是相同的。所以如果论点是一样的,那是错误的,如果它们是不同的…就像不等于运算符一样。
if ( x != 0x1 );
If fact, the only difference between the two is that !=
will return 0 or 1, while ^
will return any number, but the truthyness of the result will always be the same. An easy way to think about it is.
事实上,两者之间唯一的区别是,! =将返回0或1,^将返回任意数量,但结果的truthyness永远是相同的。一种简单的思考方式是。
(b != c) === !!(b ^ c) // for all b and c
The final "simplification" is converting 0x1
to decimal which is 1. Therefore your statement is equivalent to:
最后的“简化”是将0x1转换成十进制,也就是1。因此,你的声明相当于:
if ( x != 1 )
#15
0
^ is a bitwise XOR operator
^按位异或运算符
If x = 1
如果x = 1
00000001 (x) (decimal 1)
00000001 (0x1) (decimal 1)
XOR 00000000 (0x0) (decimal 0)
here 0 == ( x ^ 0x1 )
0 = =(x ^ 0 x1)
If x = 0
如果x = 0
00000000 (x) (decimal 0)
00000001 (0x1) (decimal 1)
XOR 00000001 (0x1) (decimal 0)
here 0 != ( x ^ 0x1 )
这里0 ! =(x ^ 0 x1)
The truth table of a xor b:
xor b的真值表:
a b a xor b
----------------------------
1 1 0
1 0 1
0 1 1
0 0 0
The code simply means
代码仅仅意味着
#16
0
As I see the answers so far miss a simple rule for handling XOR
s. Without going into details what ^
and 0x
mean (and if
, and !=
etc), the expression 0 != (x^1)
can be reworked as follows using the fact that (a^a)==0
:
在我看来,到目前为止的答案中没有一个简单的规则来处理XORs。不详细说明什么是和0x是什么意思(如果,and !=等等),表达式0 != (x 1)可以用(a a)==0的事实改写如下:
0 != (x^1) <=> [xor left and right side by 1]
(0^1) != (x^1^1) <=>
1 != x
#17
0
The standard technique that might be being used, here, is to repeat an idiom as it appears in surrounding context for clarity, rather than to obfuscate it by replacing it with an idiom that is arithmetically simpler but contextually meaningless.
这里使用的标准技术是重复一个习语,因为它出现在周围的环境中,为了清晰,而不是用一个简单的、但上下文无关的成语来代替它。
The surrounding code may make frequent reference to (x ^ 1)
, or the test may be asking "if bit 0 was the other way around, would this bit-mask be empty?".
周围的代码可能经常引用(x ^ 1),或测试可能会问“如果0是反过来,这个位元遮罩是空的吗?”。
Given that the condition causes something to be encode()
ed, it may be that in context the default state of bit 0 has been inverted by other factors, and we need only encode extra information if any of the bits deviate from their default (normally all-zero).
考虑到条件导致某些东西被编码()ed,可能在上下文环境中,比特0的默认状态被其他因素颠倒了,我们只需要编码额外的信息,如果有任何位偏离了它们的默认值(通常为零)。
If you take the expression out of context and ask what it does, you overlook the underlying intention. You might just as well look at the assembly output from the compiler and see that it simply does a direct equality comparison with 1.
如果你把这个表达从语境中去掉,然后问它做了什么,你就会忽略潜在的意图。您也可以从编译器中查看程序集的输出,并看到它只与1进行直接的相等比较。