基于使用标准逻辑运算(如AND,OR,XOR,NOT

时间:2016-12-07 18:09:43

标签: python algorithm

基于使用AND,OR,XOR,NOT等标准逻辑运算添加两个整数的算法

有人知道这个算法吗?

我似乎无法找到一个适用于Python的新手。

我只需要它帮助我正确的方向来制作我的二进制加法程序。

1 个答案:

答案 0 :(得分:1)

是。事实上,这实际上是in hardware非常标准的事情。我将在此总结其背后的逻辑。

如果你逐位执行此操作,则可以在位上使用XOR和AND。考虑添加13和6。

13 = binary 1101
6 = binary 0110

我们现在一次一个地操作位,从右到左:

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).

维基百科对此有complete truth table。在同一篇文章中,数字的逻辑是(A xor B) xor Cin,其中Cin是进位。

执行的逻辑是

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

您可以使用here符号说明查看我所描述的this数字电路图。

从更加数学的角度来看,二元自然数在加法时形成Abelian group,二元自然数形成field。 (事实上​​,十进制和二进制自然数的字段是isomorphic)。

在更切实的意义上,这意味着二进制算术的工作方式类似于十进制算术。例如,它仍然是联想和可交换的。重点是添加两个二进制数很像添加两个十进制数。考虑一下,与上面相比,添加904和117.再次,我们从右到左添加。

7 + 4 = 13.因此,结果为3,结果为1 0 + 1 = 1.还有一个随身携带,结果是2.
9 + 1 = 10.因此,结果数字为0,结果为1。

最终结果:1021。

请注意添加二进制数的相似程度。我建议尝试添加一些二进制数字"手动"只是为了更好地了解它是如何工作的 - 它实际上几乎就像十进制数的算术一样。

这是执行这些操作的C#代码,我假设这是作业,所以我将其作为练习留给"翻译"这到Python :)。 (希望语法很熟悉 - 它与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<int> resultingDigits = new List<int>();

        // 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);
    }