-1
\$\begingroup\$

I'm trying to implement the following substractor circuit in python: Substractor

And below is my code for it:

def AND(a, b):
    return a and b


def XOR(a, b):
    return a ^ b


def OR(a, b):
    return (a or b)


def full_adder(a, b, c):
    output = {
        # sum = a ⊕ b ⊕ c
        'sum' : XOR(XOR(a,b), c),

        # carry = ab + (a⊕b)c
        'carry' : OR(AND(a,b), AND(XOR(a, b), c)) 
    }
    return output   


def n_bit_binary_substractor(a, b, n):
    result = [0] * n

    k = 1 # k=1 --> substraction; K=0 --> addition

    carry = k
    
    for i in range(n-1, -1, -1):
        full_adder_output = full_adder(XOR(b[i], k), a[i], carry)
        result[i] = full_adder_output['sum']
        carry = full_adder_output['carry']
        
    output = {
        'difference'  : result, # x - y
        'carry_out': carry
    }
    return output   


# Declaring input bits
a = [0, 1, 0]
b = [0, 1 ,1]

# Declaring the no. of bits for our substractor circuit
no_of_bits = 3 

# Performing binary substraction
output = n_bit_binary_substractor(a, b, no_of_bits)
print(output)    # result: {'difference': [1, 1, 1], 'carry_out': 0}
                 # carry out is expected to be 1

According to this article:
Substracting '011' from '010' should output '111' with carry 1.
But my code ended up outputting carry '0', while the difference is same as that of the expected output.

Which part of the code is causing this problem?

\$\endgroup\$
7
  • 1
    \$\begingroup\$ This is perhaps the first time I've ever seen someone trying to use python as a HDL... \$\endgroup\$ Commented Jun 6, 2021 at 14:16
  • \$\begingroup\$ You have the code. You can run the code. You know how the code is supposed to work. You can print intermediate and internal values. You don't need us, you just need to debug your code. \$\endgroup\$ Commented Jun 6, 2021 at 14:18
  • \$\begingroup\$ I wrote this code a week ago. I've tried everything I can but still don't know what's wrong with it. \$\endgroup\$ Commented Jun 6, 2021 at 14:21
  • \$\begingroup\$ But everywhere on the internet when I look for a substractor circuit I get this same circuit, so I'm assuming there's nothing wrong in the diagram itself. And, isn't output of 010 -011 supposed to not have a carry '1' if the circuit has been correctly implemented? \$\endgroup\$ Commented Jun 6, 2021 at 14:42
  • \$\begingroup\$ Diagram is correct. Expectation is what is probably incorrect. See if amy of the web sources which give this diagram also discusses a sample problem worked out. \$\endgroup\$ Commented Jun 6, 2021 at 14:46

1 Answer 1

0
\$\begingroup\$

Answer assumes twos complement arithmetic

The sum is 010 + 100 + 1 = 010 + 101 = 111 with 0 as final carry. Your code is correct.

It is your expectation that is incorrect.

Another reason why the carry should be zero is that if the (positive) numbers where zero padded on the left and if a carry had been generated, the result would be positive number which is incorrect. Try doing the subtraction after left padding a few zeros to both numbers.

\$\endgroup\$
5
  • \$\begingroup\$ So, is the result '111' is supposed to be representing -1 in some way? \$\endgroup\$ Commented Jun 6, 2021 at 14:51
  • \$\begingroup\$ Also is the final carry output representing the sign of the result or the borrow bit or something else? \$\endgroup\$ Commented Jun 6, 2021 at 14:58
  • \$\begingroup\$ Read up on twos complement arithmetic. It may be too much to explain in comments. But basically the sign of the number can be directly read out from the MSB. Carryout probably has a different meaning compared to unsigned arithmetic. \$\endgroup\$ Commented Jun 6, 2021 at 15:17
  • \$\begingroup\$ Carry out probably shows overflow. I'll have to look it up. There is no overflow in this case since it is subtraction of two positive numbers. If the a positive number was subtracted from negative number there is a chance of overflow. \$\endgroup\$ Commented Jun 6, 2021 at 15:19
  • 1
    \$\begingroup\$ @theCursedPirate Yes, '111' is -1, because it's the maximum number and if you add one, you'll go back to '000'. Same way your bike/car trip meter works, it overflows from '9999' to '0000', so adding '9999' is same as -1. \$\endgroup\$ Commented Jun 6, 2021 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.