为什么用 AND 两个数字来得到布尔值?
我正在开发一个基于 Velleman k8055 板的小型硬件接口项目。
示例代码来自 VB.Net,我将其重写为 C#,主要是为了有机会单步执行代码并理解这一切。
但有一件事让我感到困惑:
在一个阶段,他们读取所有数字输入,然后根据读取的数字输入的答案设置一个复选框(以整数形式返回),然后他们将其与一个数字相与:
i = ReadAllDigital
cbi(1).Checked = (i And 1)
cbi(2).Checked = (i And 2) \ 2
cbi(3).Checked = (i And 4) \ 4
cbi(4).Checked = (i And 8) \ 8
cbi(5).Checked = (i And 16) \ 16
我还没有完成数字系统一段时间后,我明白他们正在尝试做什么,但是它对 AND 两个数字会产生什么影响? 不是所有大于 0 的值都等于 true 吗?
你会如何将其翻译成 C#?
I am working on a little Hardware interface project based on the Velleman k8055 board.
The example code comes in VB.Net and I'm rewriting this into C#, mostly to have a chance to step through the code and make sense of it all.
One thing has me baffled though:
At one stage they read all digital inputs and then set a checkbox based on the answer to the read digital inputs (which come back in an Integer) and then they AND this with a number:
i = ReadAllDigital
cbi(1).Checked = (i And 1)
cbi(2).Checked = (i And 2) \ 2
cbi(3).Checked = (i And 4) \ 4
cbi(4).Checked = (i And 8) \ 8
cbi(5).Checked = (i And 16) \ 16
I have not done Digital systems in a while and I understand what they are trying to do but what effect would it have to AND two numbers? Doesn't everything above 0 equate to true?
How would you translate this to C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
这是执行按位 AND,而不是逻辑与。
其中每一个基本上都确定
i
中的单个位是否被设置,例如:(因为 5 = 二进制 101,而 4、2 和 1 分别是二进制 100、010 和 001 的十进制值。 )
This is doing a bitwise AND, not a logical AND.
Each of those basically determines whether a single bit in
i
is set, for instance:(Because 5 = binary 101, and 4, 2 and 1 are the decimal values of binary 100, 010 and 001 respectively.)
我想你必须把它翻译成这样:
等等......
这是使用按位 AND 运算符。
当您使用按位 AND 运算符时,该运算符将比较两个给定值的二进制表示形式,并返回一个二进制值,其中仅设置了那些也在两个操作数中设置的位。
例如,当您这样做时:
2 & 2
它将执行以下操作:
这将导致:
然后,如果将此结果与 2 (0010) 进行比较,它当然会返回 true。
I think you 'll have to translate it to this:
etc...
This is using the bitwise AND operator.
When you use the bitwise AND operator, this operator will compare the binary representation of the two given values, and return a binary value where only those bits are set, that are also set in the two operands.
For instance, when you do this:
2 & 2
It will do this:
And this will result in:
Then if you compare this result with 2 (0010), it will ofcourse return true.
只是补充一下:
这称为位掩码
http://en.wikipedia.org/wiki/Mask_(computing)
布尔值只需要 1 位。 在大多数编程语言的实现中,布尔值需要多个位。 在PC中这不会造成很大的浪费,但是嵌入式系统通常内存空间非常有限,所以浪费确实很大。 为了节省空间,布尔值被打包在一起,这样一个布尔变量只占用 1 位。
您可以将其视为执行类似于数组索引操作的操作,其中一个字节(= 8 位)变得像一个由 8 个布尔变量组成的数组,所以也许这就是您的答案:使用布尔数组。
Just to add:
It's called bitmasking
http://en.wikipedia.org/wiki/Mask_(computing)
A boolean only require 1 bit. In the implementation most programming language, a boolean takes more than a single bit. In PC this won't be a big waste, but embedded system usually have very limited memory space, so the waste is really significant. To save space, the booleans are packed together, this way a boolean variable only takes up 1 bit.
You can think of it as doing something like an array indexing operation, with a byte (= 8 bits) becoming like an array of 8 boolean variables, so maybe that's your answer: use an array of booleans.
以二进制形式思考这一点,例如,
产生
00000010
,即不为零。 现在,如果第一个值是
你会得到
零。
注意进一步的除法,将所有值都减为 1 或 0。
Think of this in binary e.g.
yields
00000010
i.e. not zero. Now if the first value was
you'd get
i.e. zero.
Note the further division to reduce everything to 1 or 0.
(i 和 16) / 16 提取第 5 位的值(1 或 0)。
(i and 16) / 16 extracts the value (1 or 0) of the 5th bit.
And 运算符执行“...按位合取”在两个数字表达式上”,映射到“|” 在 C# 中。 '` 是一个整数除法,在 C# 中相当于 < code>/,前提是两个操作数都是整数类型。
And operator performs "...bitwise conjunction on two numeric expressions", which maps to '|' in C#. The '` is an integer division, and equivalent in C# is
/
, provided that both operands are integer types.常量数字是掩码(以二进制形式思考它们)。 因此,代码的作用是对字节和掩码应用 按位 AND 运算符,然后除以数字,以获得位。
例如:
The constant numbers are masks (think of them in binary). So what the code does is apply the bitwise AND operator on the byte and the mask and divide by the number, in order to get the bit.
For example:
在 C# 中,使用 BitArray 类 直接索引各个位。
设置单个位 i 很简单:
重置单个位 i 有点尴尬:
请注意,按位运算符和移位运算符都倾向于提升所有内容到
int
,这可能会意外地需要转换。In C# use the BitArray class to directly index individual bits.
To set an individual bit i is straightforward:
To reset an individual bit i is a little more awkward:
Be aware that both the bitwise operators and the shift operators tend to promote everything to
int
which may unexpectedly require casting.正如所说,这是按位与,而不是逻辑与。 我确实看到这在我之前已经说过很多次了,但在我看来,这些解释并不那么容易理解。
我喜欢这样想:
将二进制数写在彼此下面(这里我写的是 5 和 1):
现在我们需要将其转换为二进制数,其中所有 1 都来自第一个数字,即也在第二个中被转移,也就是说 - 在这种情况下:在这种
情况下,我们看到它给出与第二个数字相同的数字,其中该操作(在VB中)返回true。 让我们看一下其他示例(使用 5 作为 i):
(5 和 2)
(false)
(5 和 4)
(true)
(5 和 8)
(false)
(5 和 16)
(false)
编辑:显然我错过了问题的全部要点 - 这是 C# 的翻译:
As said this is a bitwise AND, not a logical AND. I do see that this has been said quite a few times before me, but IMO the explanations are not so easy to understand.
I like to think of it like this:
Write up the binary numbers under each other (here I'm doing 5 and 1):
Now we need to turn this into a binary number, where all the 1's from the 1st number, that is also in the second one gets transfered, that is - in this case:
In this case we see it gives the same number as the 2nd number, in which this operation (in VB) returns true. Let's look at the other examples (using 5 as i):
(5 and 2)
(false)
(5 and 4)
(true)
(5 and 8)
(false)
(5 and 16)
(false)
EDIT: and obviously I miss the entire point of the question - here's the translation to C#:
我更喜欢在位调整时使用十六进制表示法(例如 0x10 而不是 16)。 当您增加位深度时,它会更有意义,因为 0x20000 比 131072 更好。
I prefer to use hexadecimal notation when bit twiddling (e.g. 0x10 instead of 16). It makes more sense as you increase your bit depths as 0x20000 is better than 131072.