在这种情况下,“加法"和“按位或"是否相同? [英] Are 'addition' and 'bitwise or' the same in this case?

查看:111
本文介绍了在这种情况下,“加法"和“按位或"是否相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有四个32位数字,它们被定义为使它们的位不重叠,即

Say I have four 32-bit numbers, defined so that their bits don't overlap, i.e.

unsigned long int num0 = 0xFF000000;
unsigned long int num1 = 0x00FF0000;
unsigned long int num2 = 0x0000FF00;
unsigned long int num3 = 0x000000FF;

每个数字中的任何地方都可以代替FF.

Where in each number one could have anything in the place of the FFs.

我是说对了吗?加法按位或对于此类数字总是会产生相同的输出?

Am I right in saying that addition and bitwise or would always produce the same output for such sort of numbers?

谢谢!

推荐答案

只要两个数字num1和num2适用num1 & num2 == 0,则遵循:

as long as for two numbers num1 and num2 applies num1 & num2 == 0, then follows:

num1 + num2 == num1 | num2

这样做的原因是,加法基本上是按位XOR, plus 进位.但是只要没有进位位(num1 & num2 == 0),加法就会归结为按位异或,在这种情况下(再次由于num1 & num2 == 0)在逻辑上等效于按位OR

the reason for this is, that addition is basically a bitwise XOR, plus carry bit. But as long as there are no carry bits (num1 & num2 == 0) then addition boils down to bitwise XOR, which is (again because of num1 & num2 == 0) in this case logically equivalent to a bitwise OR

这篇关于在这种情况下,“加法"和“按位或"是否相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆