C#中按位与的逆是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/783237/
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-05 00:59:06  来源:igfitidea点击:

What is the inverse of bitwise AND in C#?

c#bitwise-operators

提问by Ivan Prodanov

I need to do an inverse calculation, which consists bitwise AND operation,how do I do it?

我需要做一个逆计算,它包含按位与运算,我该怎么做?

I tried exclusive OR, but it didn't help.

我尝试了异或,但没有帮助。

        int i = 254 & 2;
        Console.Write("254 & 2 ={0}", i + "\n");
        Console.Write("{0} ^ 2 ={1}",i, (i ^ 2) + "\n");

Doesn't work. How do I do that calculation?

不起作用。我怎么做这个计算?

采纳答案by Samuel

Given i, you cannot get back 254. By &ing it you have destroyed what data was not stored in the second bit.

鉴于i,你不能回来254。通过&ing 它,您已经破坏了未存储在第二位中的数据。

 1111 1110
&0000 0010
----------
 0000 0010

How would you recover the 6 lost bits? For x & 2 == 2, you could put almost any xand it would be true.

您将如何恢复丢失的 6 位?对于x & 2 == 2,您几乎可以输入任何内容x,这将是正确的。

 0010 1010 // = 42
&0000 0010
----------
 0000 0010

Is x254 or 42? You cannot tell.

x254 还是 42?你不能说。

回答by Beardo

You can't, you've lost the data that was there when you did the &.

你不能,你已经丢失了执行 & 时存在的数据。

4-bit example:

4 位示例:

1110 & 0010 = 0010

You have no way of knowing which bits were 1 and which weren't if you only know the result 0010 and the second operand of the & (also 0010).

如果您只知道结果 0010 和 & 的第二个操作数(也是 0010),您就无知道哪些位是 1,哪些不是。

回答by Adam Davis

Technically the opposite of AND is NAND:

从技术上讲,AND 的对立面是 NAND:

~( 254 & 2 )

~( 254 & 2 )

Note that the ~ is the complement operator, and does a bitwise NOT (toggle each bit to its opposite).

请注意, ~ 是补码运算符,并按位非(将每一位切换到相反的位)。

What exactly do you want, though? What are you trying to accomplish?

不过,你到底想要什么?你想达到什么目的?

If you're trying to undo the calculation, you can't - there is no inverse function such that inverseand(and(x, y)) will return x or y, even if inverse is given one of them.

如果您试图撤消计算,则不能 - 没有反函数可以使 inverseand(and(x, y)) 返回 x 或 y,即使给定了 inverse 之一。

-Adam

-亚当

回答by Crippledsmurf

Wikipedia has a good article on bitwise operations, how they work and their syntax in C and Java which is very similar to C#

维基百科有一篇关于按位运算的好文章,它们是如何工作的,以及它们在 C 和 Java 中的语,这与 C# 非常相似

http://en.wikipedia.org/wiki/Bitwise_operation

http://en.wikipedia.org/wiki/Bitwise_operation

MSDN of course also has documentation for each of the bitwise and logical operators each of which have an example:

MSDN 当然也有每个按位和逻辑运算符的文档,每个运算符都有一个示例:

http://msdn.microsoft.com/en-us/library/6a71f45d(vs.71).aspx

http://msdn.microsoft.com/en-us/library/6a71f45d(vs.71).aspx

回答by Jonas Malm

What do you mean by opposite calculation?

你说的相反计算是什么意思?

If you see the number 254 as an bit register consisting of 8 bits then all bits but the last is set to 1.

如果您将数字 254 视为由 8 位组成的位寄存器,则除最后一位以外的所有位都设置为 1。

Calculating 254 & 2 is the same as checking whether bit number 2 in the register is set.

计算 254 & 2 与检查寄存器中的第 2 位是否设置相同。

What is the opposite of this? Checking if all other bits are set?

与此相反的是什么?检查是否设置了所有其他位?

回答by Alnitak

If the purpose of the &operation is to check whether bit 1 is set, then a potential "opposite" operation is 'set bit 1'.

如果&操作的目的是检查位 1 是否已设置,则潜在的“相反”操作是“设置位 1”。

i.e.:

IE:

val = val | 2;

This overwrites the value currently in bit 2, and doesn't touch any other bit.

这会覆盖当前位 2 中的值,并且不会触及任何其他位。

If the 8 bits in the byte are considered to be completely independent bits then it's possible to change any of them with touching any of the others.

如果字节中的 8 位被认为是完全独立的位,则可以通过接触任何其他位来更改其中任何一位。

In this case, it doesn't matter that some of the original information was lost. We don't actually care what value the other bits had, and most often when using bit masks the original value of the bit in question was zero anyway.

在这种情况下,丢失一些原始信息并不重要。我们实际上并不关心其他位的值,而且大多数情况下,在使用位掩码时,相关位的原始值无论如何都是零。