4. Implement adder and subtraction using logic gate

Source: Internet
Author: User

Understanding the door, we can do some practical applications, the main purpose of the computer is calculated, and the door can be used for calculation, so it can be said that the computer is composed of a variety of different doors. The next thing we do is make a binary adder, because if you make an adder, you can extend it to subtraction, multiplication, division. And how do we implement the adder? We only use some of the most common and somewhat backward parts: switches, bulbs, wires, batteries, doors.

The input is a switch, the output is a row of light bulbs, the light indicates 1, the kill represents 0. If the binary string is additive, we will definitely think of a single one for addition, so if there are 8-bit binary numbers, we'll have to do 8-part operations. By trying to know, only when the input is two 1 o'clock, the carry bit is 1, so it conforms to the gate characteristics, and when the input is two 1 or two 0 o'clock, the addition bit will be 0. This conforms to the XOR or characteristic. Different or how we use the most basic logic gate to express it. We can make it through two doors, one or the other. This makes up a half additive.

It is called a half adder because we do not consider the low carry problem, and if we consider it, we are called a full adder. The full additive is made up of 2 halves and one or a door.

Let's talk about the total number of relays to use:

The carry bit is with the door, so it consists of 2 relays.

The addition bit is one or the door, one with the non door, one with the door composed, so by 6 relays.

The half adder consists of a carry bit and an addition bit, then there are 8 relays.

The full additive is composed of two halves and one or a door, then there are 18 relays.

We have a full adder, then we can form the addition of a binary string by cascading.

But now the computer is using transistors, so you need to switch the relays into transistors.

We implemented the adder, as we said, subtraction is an extension of addition, so the adder is implemented, only a little modification, you can achieve subtraction, and subtraction is the most annoying is borrow, so we think there is a way to avoid borrow it.
The answer is yes,
(1) When meiosis > meiosis, set to be meiosis X, meiosis for y, assuming that x and Y are three-bit decimal numbers, only need to x-y=x+ (999-y) +1-1000, this formula looks very ordinary, but can avoid borrow problem. Because 999-y cannot happen borrow, other steps are not possible.
(2) x-y=-(999-(x+ (999-y)) when being meiosis < meiosis, so as to avoid borrow.
Here we implement binary subtraction, so it's similar to the above,
(1) x-y=x+ (111-y) +1-1000 when being meiosis > meiosis.
(2) x-y=-(111-(x+ (111-y)) when being meiosis < meiosis.
So knowing the method, using the adder how do we achieve it? Here we have only achieved the first situation.
We split the x+ (11...1-y) +1-100...0 to see
(1) First a=11...1-y, this is actually the negation, that is, through the former said inverter.
(2) b=x+ (111...1-y) +1, this only need the front adder.
(3) b-100 ... 0 This only need to ignore the highest bit. Because of the meiotic > meiosis, the highest position is definitely 1.
If we can't do a subtraction alone, we can combine the adder with the subtraction.
We first need to have a switch that is the choice of addition and subtraction. If it is addition, do it according to the adder, and if it is subtraction, do it in the subtraction mode.
We set this selection to X, subtraction to 1, and addition to 0. This bit is different from each bit of input or, if it is addition, the input and input is inconvenient, if it is subtraction, then the input is the opposite of the output, that is, the 2 complement. This achieves the 11 ... The 1-y function.
We also have to set the minimum carry Y, if it is subtraction, then 1, if it is addition, then 0. This solves the x+1+ (11...1-y).
We set the highest bit to Z, if it is addition, the output is unchanged, if it is subtraction, the output becomes 0. We need to use a different or a door. The x+ (111...1-y) +1-1000..0 is realized.
This enables us to combine the subtraction device with the adder.
Next, we'll introduce a few terms:
(1) If the number is set to X, then the complement of the 999-x is 10, then the 111-x is the complement of 2.
We have another question as to how to express subtraction.
Many people know that there are original code, inverse code, complement.
For example, 111 represents 1 in a signed number. The unsigned number represents +3. So the original binary system does not have any meaning, only to give him the context, will have meaning.
We also need to consider the overflow problem when the signed number is added. If, for example, 011+010=101, the original positive number becomes negative because he is beyond the range that the complement can represent.

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.