与整数的布尔运算

发布于 2024-07-10 02:20:52 字数 340 浏览 9 评论 0原文

这可能是非常基本的......但我似乎不明白:

如何

(2 & 1) = 0
(3 & 1) = 1
(4 & 1) = 0

等等......

上面的这种模式似乎有助于找到偶数,

或者

(0 | 1) = 1
(1 | 1) = 1
(2 | 1) = 3
(3 | 1) = 4
(4 | 1) = 5
(5 | 1) = 5

我知道布尔代数如何在位之间工作。 但我不明白布尔代数如何与整数一起工作(至少在 C# 中)。

提前致谢。

This is probably pretty basic... but I don't seem to get it:

How does

(2 & 1) = 0
(3 & 1) = 1
(4 & 1) = 0

etc..

This pattern above seems to help find even numbers

or

(0 | 1) = 1
(1 | 1) = 1
(2 | 1) = 3
(3 | 1) = 4
(4 | 1) = 5
(5 | 1) = 5

I know how boolean algebra works between bits. But I don't understand how Boolean algebra works with integers (in C# at the least).

thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沦落红尘 2024-07-17 02:20:52

您得到第一个结果是因为您正在两个数字的位字符串之间执行布尔 and

2 & 1 => 010 & 001 = 000 = 0
3 & 1 => 011 & 001 = 001 = 1
4 & 1 => 100 & 001 = 000 = 0
5 & 1 => 101 & 001 = 001 = 1

实际上,您正在测试是否设置了“1”位,这仅适用于奇数。

执行or操作时:

0 | 1 => 000 | 001 = 001 = 1
1 | 1 => 001 | 001 = 001 = 1
2 | 1 => 010 | 001 = 011 = 3
3 | 1 => 011 | 001 = 011 = 3
4 | 1 => 100 | 001 = 101 = 5
5 | 1 => 101 | 001 = 101 = 5

由于在这种情况下or的效果是始终设置1位,因此偶数将增加1到最接近的较大奇数。

You are getting the first result because you are performing a boolean and between the bit strings of the two numbers:

2 & 1 => 010 & 001 = 000 = 0
3 & 1 => 011 & 001 = 001 = 1
4 & 1 => 100 & 001 = 000 = 0
5 & 1 => 101 & 001 = 001 = 1

In effect, you are testing whether the '1' bit is set, which will only be true for odd numbers.

When performing or operations:

0 | 1 => 000 | 001 = 001 = 1
1 | 1 => 001 | 001 = 001 = 1
2 | 1 => 010 | 001 = 011 = 3
3 | 1 => 011 | 001 = 011 = 3
4 | 1 => 100 | 001 = 101 = 5
5 | 1 => 101 | 001 = 101 = 5

Since in this case the effect or the or is to always set the 1 bit, even numbers will be incremented by one to their nearest greater odd number.

沙沙粒小 2024-07-17 02:20:52

它在 C# 中的工作方式与在二进制中的工作方式相同。

<代码>2 | 1 = 3 和 4 | 1 = 5。

要理解这一点,您需要考虑 1、2、3、4 和 5 的二进制表示形式:

010 | 001 = 011 和 100 | 001 = 101。

同样:

010 & 001 = 000011 & 001 = 001

It works the same way in C# as it does in binary.

2 | 1 = 3 and 4 | 1 = 5.

To understand this, you need to think about the binary representation of 1,2,3,4,and 5:

010 | 001 = 011 and 100 | 001 = 101.

Similarly:

010 & 001 = 000 and 011 & 001 = 001

枫林﹌晚霞¤ 2024-07-17 02:20:52

关键是 CPU 正在并行执行 32 个布尔运算,每个布尔运算对应输入整数的每个位位置(当然假设是 32 位整数)。

The key is that the CPU is doing 32 boolean operations in parallel, one for each bit position of the input integers (assuming 32 bit integers, of course).

你丑哭了我 2024-07-17 02:20:52

它对整数进行按位运算。 它正在对第一个整数中的每个位与另一个整数中的相应位进行逻辑或/与。 然后它返回所有这些操作的结果。 例如,4 = 0100 和 1 = 0001,这些逻辑与将按顺序对位进行位运算并得到 0000(因为 0&0 = 0、1&0 = 0、0&0 = 0 和 0&1 = 0)。 对于 or,您将得到 0101(因为 0|0 = 0、1|0 = 1、0|0 = 0 和 0|1 = 1)。 诀窍在于,这些是按位运算,而不是仅对 C# 中的布尔值进行运算的逻辑运算。

It is doing bitwise operations on the integer. That it is doing a logical or/and of each bit in the first integer with the corresponding bit in the other integer. It then returns the result of all of these operations. For example, 4 = 0100 and 1 = 0001, a logical and of these would and bit the bits in order and get 0000 (since 0&0 = 0, 1&0 = 0, 0&0 = 0, and 0&1 = 0). For or, you would get 0101 (since 0|0 = 0, 1|0 = 1, 0|0 = 0, and 0|1 = 1). The trick is that these are bitwise operations, not logical operations which operate only on boolean values in C#.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文