按位运算是否分布于加法?

发布于 2024-08-10 18:14:07 字数 239 浏览 21 评论 0原文

我正在研究一个我正在尝试优化的算法,它基本上是很多麻烦,然后在严格的反馈中添加了一些内容。如果我可以对加法器使用进位保存加法,它确实会帮助我加快速度,但我不确定是否可以将操作分配给加法。

具体来说,如果我表示:

  a = sa+ca  (state + carry)
  b = sb+cb

我可以用s和c表示(a>>>r)吗? 怎么样? b和a&乙?

I'm looking at an algorithm I'm trying to optimize, and it's basically a lot of bit twiddling, followed by some additions in a tight feedback. If I could use carry-save addition for the adders, it would really help me speed things up, but I'm not sure if I can distribute the operations over the addition.

Specifically if I represent:

  a = sa+ca  (state + carry)
  b = sb+cb

can I represent (a >>> r) in terms of s and c?
How about a | b and a & b?

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

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

发布评论

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

评论(2

暮倦 2024-08-17 18:14:07

想想看...

sa = 1    ca = 1
sb = 1    cb = 1
a = sa + ca = 2
b = sb + cb = 2
(a | b) = 2
(a & b) = 2
(sa | sb) + (ca | cb) = (1 | 1) + (1 | 1) = 1 + 1 = 2 # Coincidence?
(sa & sb) + (ca & cb) = (1 & 1) + (1 & 1) = 1 + 1 = 2 # Coincidence?

让我们尝试一些其他值:

sa = 1001   ca = 1   # Binary
sb = 0100   cb = 1
a = sa + ca = 1010
b = sb + cb = 0101
(a | b) = 1111
(a & b) = 0000
(sa | sb) + (ca | cb) = (1001 | 0101) + (1 | 1) = 1101 + 1 = 1110 # Oh dear!
(sa & sb) + (ca & cb) = (1001 & 0101) + (1 & 1) = 0001 + 1 = 2    # Oh dear!

因此,通过 4 位计数器示例证明您不能将 AND 或 OR 分配给加法。

'>>>' 怎么样? (无符号或逻辑右移)。使用最后一个示例值,并且 r = 1:

sa = 1001
ca = 0001
sa >>> 1 = 0101
ca >>> 1 = 0000
(sa >>> 1) + (ca >>> 1) = 0101 + 0000 = 0101
(sa + ca) >>> 1 = (1001 + 0001) >>> 1 = 1010 >>> 1 = 0101  # Coincidence?

让我们看看这是否也是巧合:

sa = 1011
ca = 0001
sa >>> 1 = 0101
ca >>> 1 = 0000
(sa >>> 1) + (ca >>> 1) = 0101 + 0000 = 0101
(sa + ca) >>> 1 = (1011 + 0001) >>> 1 = 1100 >>> 1 = 0110  # Oh dear!

再次通过反例证明。

因此逻辑右移也不具有加法的分配性。

Think about it...

sa = 1    ca = 1
sb = 1    cb = 1
a = sa + ca = 2
b = sb + cb = 2
(a | b) = 2
(a & b) = 2
(sa | sb) + (ca | cb) = (1 | 1) + (1 | 1) = 1 + 1 = 2 # Coincidence?
(sa & sb) + (ca & cb) = (1 & 1) + (1 & 1) = 1 + 1 = 2 # Coincidence?

Let's try some other values:

sa = 1001   ca = 1   # Binary
sb = 0100   cb = 1
a = sa + ca = 1010
b = sb + cb = 0101
(a | b) = 1111
(a & b) = 0000
(sa | sb) + (ca | cb) = (1001 | 0101) + (1 | 1) = 1101 + 1 = 1110 # Oh dear!
(sa & sb) + (ca & cb) = (1001 & 0101) + (1 & 1) = 0001 + 1 = 2    # Oh dear!

So, proof by 4-bit counter example that you cannot distribute AND or OR over addition.

What about '>>>' (unsigned or logical right shift). Using the last example values, and r = 1:

sa = 1001
ca = 0001
sa >>> 1 = 0101
ca >>> 1 = 0000
(sa >>> 1) + (ca >>> 1) = 0101 + 0000 = 0101
(sa + ca) >>> 1 = (1001 + 0001) >>> 1 = 1010 >>> 1 = 0101  # Coincidence?

Let's see whether that is coincidence too:

sa = 1011
ca = 0001
sa >>> 1 = 0101
ca >>> 1 = 0000
(sa >>> 1) + (ca >>> 1) = 0101 + 0000 = 0101
(sa + ca) >>> 1 = (1011 + 0001) >>> 1 = 1100 >>> 1 = 0110  # Oh dear!

Proof by counter-example again.

So logical right shift is not distributive over addition either.

我们只是彼此的过ke 2024-08-17 18:14:07

不可以,您不能将 AND 或 OR 分配给二元运算符。

解释

设P是一个命题,其中P:(A+B)&C = A&C + B&C

让我们取A=2,B=3 =>A+B=5 。

我们要证明 A&C + B&C != (A+B)&C

A=2= 010

B=3= 011

让 010&C = x,
其中 x 是某个整数,其值是 010 和 C 的按位 AND 的结果

,类似地 011&C = y,其中 y 是某个整数,其值是 011 和 C 的按位 AND 的结果,

因为我们不能说 P 对于所有 C 都成立在自然数集合 ( {0,1,...} ) 中,因此 P 为假。

在这种情况下,取C=2=010

x=010 & 010 = 010 = 2

y=011 & 010 = 010 = 2

5&2=101& 010 = 000 = 0

显然, x+y!=0 ,这意味着 (A+B)&C != A&C + B&C。

由此证明!

No, you cannot distribute AND or OR over binary operators.

Explanation

Let P be a proposition where P: (A+B)&C = A&C + B&C

let us take A=2,B=3 =>A+B=5.

We are to prove A&C + B&C != (A+B)&C

A=2= 010

B=3= 011

let 010&C = x,
where x is some integer whose value is the resultant of bitwise AND of 010 and C

similarly 011&C = y, where y is some integer whose value is the resultant of bitwise AND of 011 and C

since we cannot say P holds for all C in the set of Natural numbers ( {0,1,...} ), consequently P is false.

In this case, take C=2=010

x=010 & 010 = 010 = 2

y=011 & 010 = 010 = 2

5&2=101 & 010 = 000 = 0

clearly, x+y!=0 , which means (A+B)&C != A&C + B&C.

Hence proved!

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