python and or xor_用于基于标准逻辑运算(例如AND,OR,XOR,NOT)的两个整数相加的算法...

Algorithm for adding two integers based on the use of standard logical operations like AND, OR, XOR, NOT

Does anyone know an algorithm for this?

I can't seem to find one that works as I am very new to Python.

I just need to it to help me in the right direction to make my binary addition program.

解决方案

Yes. In fact, this is actually a pretty standard thing to do in hardware. I'll give a summary of the logic behind it here.

If you do this bit-by-bit, you can just use XOR and AND on the bits. Consider adding 13 and 6.

13 = binary 1101

6 = binary 0110

We now operate on the bits one at a time, right to left:

1 xor 0 = 1, 1 and 0 = 0 (no carry). "Current" result: 1

0 xor 1 = 1, 1 and 0 = 0 (no carry). "Current" result: 11

1 xor 1 = 0, 1 and 1 = 1 (there *is* a carry "out") "Current" result: 011

There's a carry in, so this is 1 xor 1 xor 0, which is 0. The carry out is 1. Current result: 0011

Next, we need to add the carry-out. Thus, the final number is 10011 (19).

Wikipedia has the complete truth table for this. From the same article, the logic for the digit is (A xor B) xor Cin where Cin is the carry in.

The logic for the carry out is

((A xor B) and Cin) or (A and B)

You can see a diagram of the digital circuit that I'm describing here, using this description of the symbols.

From a more mathematical perspective, binary natural numbers form an Abelian group under addition, and binary natural numbers form a field. (In fact, the fields of decimal and binary natural numbers are isomorphic).

What this means in a more tangible sense is that binary arithmetic works in a way that's analogous to decimal arithmetic. It's still associative and commutative, for example. Point being that adding two binary numbers is a lot like adding two decimal numbers. Consider, for comparison to above, adding 904 and 117. Again, we add right to left.

7 + 4 = 13. Thus, the result is 3 and the carry-out is 1.

0 + 1 = 1. There's also a carry-in, so the result is 2.

9 + 1 = 10. Thus, the resulting digit is 0 and the carry-out is 1.

Final result: 1021.

Note how similar that was to adding the binary numbers. I'd suggest trying to add a few binary numbers "by hand" just to get more of a feel for how it works - it's almost exactly like arithmetic on decimal numbers actually.

Here's C# code to perform these operations, I'm assuming this is homework so I'll leave it as an exercise to "translate" this into Python :). (Hopefully the syntax is familiar - it's very similar to Java).

private static string LeftPad(string array, int length)

{

var sb = new StringBuilder();

for (int i = 0; i < (length - array.Length); i++)

{

sb.Append(0);

}

sb.Append(array);

return sb.ToString();

}

private static int AddBits(int num1, int num2)

{

// Convert the numbers to binary (base-2) strings

string num1Bits = Convert.ToString(num1, 2);

string num2Bits = Convert.ToString(num2, 2);

// Track the current carry-in/carry-out

int carry = 0;

// If the strings are of differing lengths, left-pad the shorter one with zeros

string num1ToAdd = (num1Bits.Length >= num2Bits.Length ? num1Bits : LeftPad(num1Bits, num2Bits.Length));

string num2ToAdd = (num2Bits.Length >= num1Bits.Length ? num2Bits : LeftPad(num2Bits, num1Bits.Length));

List resultingDigits = new List();

// Loop through the strings from right to left and perform the operation listed above

for (int i = num1ToAdd.Length - 1; i >= 0; i--)

{

// Digits we are currently operating on

int A = int.Parse(num1ToAdd[i].ToString());

int B = int.Parse(num2ToAdd[i].ToString());

int result = (A ^ B) ^ carry;

resultingDigits.Add(result);

carry = ((A ^ B) & carry) | (A & B);

}

// If there's a carry, add that as well

if (carry == 1)

resultingDigits.Add(1);

// Change the endianness

resultingDigits.Reverse();

var sb = new StringBuilder();

for (int i = 0; i < resultingDigits.Count; i++)

{

sb.Append(resultingDigits[i]);

}

// Convert the base-2 (binary) string to a regular int

return Convert.ToInt32(sb.ToString(), 2);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值