Python implements binary adder

Source: Internet
Author: User

These two days to see the "Secret of the Code" inside the binary addition machine and its subsequent subtraction function implementation, using Python to implement a similar function of the adder out.

Let's talk about the whole idea first.

Since the operands are both binary, the calculation is much simpler. First, the operation requires bitwise operation, the two binary numbers are added using Andgate, but the focus is to distinguish between "and" and "carry", respectively, using xorgate and andgate implementation, which is the half adder. Then, you need to consider the carry of the right bit, so you need a CI (carry in), and then put the standard sum and the right bit of carry in again into a half adder inside. Then, to understand whether the first two numbers produce a carry, or because a carry input has been generated, in short, there may only be one carry, so the co (carry out) of the two half adder gets the final rounding through Orgate, The second half of the add-on and is the final and. This will take care of the full add-on. Such as:

And then all you need to do is connect the eight identical:

The implementation of subtraction is a little more complicated, because subtraction exists borrow, so the primary idea is to replace subtraction with addition, so for a-B, it needs to be replaced with a (A + (11111111-b) +1)-100000000 form, so that 11111111-b will not borrow. Then is how to deal with the 11111111-b, which requires the anti-code of B (also known as 1 of the complement), that is, the 1 change 0,0 1, the implementation of the need to use the reverse device, but considering the implementation with addition, it is necessary to use a xorgate and a flag sub to indicate whether the addition or subtraction operations, Also used in the first through the CI implementation plus 1 and in the final step to the first 1 (please read the process). The main illustrations are as follows:

So far, it's all done.

Then I replace the definition of the logic gate with the definition of the function in Python, and write the following adder:

1 #!/usr/bin/env python2 3 defand (A, b):4     returnInt (A andb)5 6 defOr (A, b):7     returnInt (Aorb)8 9 defNand (A, b):Ten     returnint notand (A, b)) One  A defXor (A, b): -     returnand (Nand (A, B), Or (A, b)) -  the defHalf_adder (A, b): -s =Xor (A, b) -CO =and (A, b) -     returnS, CO +  - defFull_adder (A, B, CI): +s, Co1 =Half_adder (A, b) As, CO2 =Half_adder (CI, s) atCO =Or (co1, CO2) -     returnS, CO -  - defEight_bit_adder (x, Y, sub):#Sub=0:add, Sub=1:subtract -y =list (y) -      forIinchRange (len (y)): inY[i] =Xor (Sub, y[i]) -Ans = [Full_adder (int (x[7]), int (y[7]), sub)] to      forIinchRange (6,-1, 1): +Ans.insert (0, Full_adder (int (x[i)), int (y[i]), ans[0][1])) -Ans.insert (0, (Xor (Sub, ans[0][1]), None)) the      forEachbitinchans: *         PrintEachbit[0], $ Panax Notoginseng if __name__=='__main__': -     Print 'Select an operator and enter the binary numbers to get their sum or diff' theo = Raw_input ('(A) dd/(S) Ubtract:'). Strip (). Lower () [0] +x = Raw_input ('x:') Ay = raw_input ('y:') the     ifo = ='a': +Sub =0 -     elifo = ='s': $Sub = 1 $Eight_bit_adder (x, Y, sub)

At the same time, the implementation of subtraction chapter finally, how to use the binary representation of the signed number, but also let me to the computer inside the world more understanding. Let's talk about it here today.

Python implements binary adder

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.