As we all know, in the computer, all data are represented by electrical signals, and natural addition calculation is also used to express the process and results by electrical signals. High level represents 1, low level represents 0, which is binary number. If we want to calculate 11+21=? , which is 1011+10101=?

Why use binary

Known electrical signals by the control current of diode output, according to the computer by controlling the output current of the diode size corresponding to different status value, early calculator appeared corresponding to three kinds of states, the state of the five circuit, namely ternary computer, five into the computer, but the state, the more the more difficult it is to distinguish the signal, when there is interference outside, Electrical signals may be confused together, and the transistor itself is a high-speed component, which will make the confusion more serious. In order to minimize the occurrence of such problems, only high level and low level represent two states, namely on and off, which greatly reduces the possibility of error.

The basic operators are NOR, AND, OR, AND the output values are true AND false, which exactly match the transistor circuit control on AND off. The truth table of each basic logic gate is as follows:

Half adder and full adder

When 10111+10101 is added for the first time, it is necessary to calculate the addition of two binary numbers, 1 and 1. According to the principle of full two carry=1, sum=0, carry=1, corresponding to the circuit, two input bits A and B. Two output bits, SUM and CARRY.

By observation, it can be found that sum=0 when both input bits A AND B are 1 or 0, AND sum=1 at other times exactly corresponds to the output of XOR gate. Therefore, the output of XOR is sum. In addition, carry=1 is required only when both input bits A AND B are 1, which corresponds to the output of AND gate. Therefore, the output of AND gate is CARRY, AND the whole circuit is half adder:


Corresponding JS code:

// Half adder, & represents and gate, ^ represents XOR gate
const halfAdder = (a: number, b: number) = > [a & b, a ^ b];
Copy the code

After calculating the lowest value, move to the left and calculate 1+0 in 10111+10101. At this time, two addends 1 and 0 need to be calculated, plus the carry1 in the lowest value, to output a result sum and a carry carry. Based on the implementation of the half-adder, sum1 and carry1 of a+b need to be calculated first. Sum2 is the final sum output. Finally, the OR gate is used to judge whether carry2 of the second half-adder operation and carry1 of the first operation have carry. As long as there is carry, carry=1 is output, which is the whole circuit:

Corresponding JS code:

/ / full adder, | represents an OR gate
const fullAdder = (a: number, b: number, carry: number) = > (
  ([a, b] = halfAdder(a, b)), ([carry, b] = halfAdder(b, carry)), [a | carry, b]
);
Copy the code

With half adder and full adder, it is only necessary to link the input of each bit to the corresponding half adder or full adder, and then turn on the power supply to get the final output, the circuit is:

Code implementation

With a basic implementation, you can simply write code according to logic:

/ / a half adder
const halfAdder = (a: number, b: number) = > [a & b, a ^ b];

/ / full adder
const fullAdder = (a: number, b: number, carry: number) = > (
  ([a, b] = halfAdder(a, b)), ([carry, b] = halfAdder(b, carry)), [a | carry, b]
);

// bit adding calculator
const bitsAdder = (addend1: number, addend2: number) = > {
  let addend1Bits: string = addend1.toString(2);
  let addend2Bits: string = addend2.toString(2);
  let maxLength = 0;
  // 补0
  addend1Bits.length >= addend2Bits.length
    ? ((maxLength = addend1Bits.length - 1), (addend2Bits = addend2Bits.padStart(addend1Bits.length, '0')))
    : ((maxLength = addend2Bits.length - 1), (addend1Bits = addend1Bits.padStart(addend2Bits.length, '0')));
  // Avoid creating variables repeatedly
  let aBit = 0;
  let bBit = 0;
  let tempCarry = 0;
  let tempSum = 0;
  let result: number[] = [];
  let index = maxLength;
  while (index > -1) {
    aBit = +addend1Bits[index];
    bBit = +addend2Bits[index];
    [tempCarry, tempSum] = index === maxLength ? halfAdder(aBit, bBit) : fullAdder(aBit, bBit, tempCarry);
    result.unshift(tempSum);
    index--;
  }
  Remember to insert the last carry
  result.unshift(tempCarry);
  console.log(parseInt(result.join(' '), 2));
};
Copy the code