AI For DIY

Saturday, August 13, 2022

Elementary arithmetic

1. About elementary arithmetic

Arithmetic operations usually mean addition, subtraction, multiplication, and division. Even if a decimal number is converted to a binary number, the calculation method is the same, and the result value is also the same when converted between them. The difference is that there is no way to express a negative number separately when using a computer, so it is used by converting it to 2's complement with a sign in the highest digit, so be aware of this. As a result, the number that can be expressed is limited, and the number of digits to be calculated becomes important. Here, 8 digits that can be expressed as negative numbers are the standard.

 

2. Addition operation

It is the most basic of the four arithmetic operations and is simple. As an example of addition, we would add addend 000001012 to augend 011010012.

 

) 011010012 (10510)

) 000001012 (510)

) 011011102 (11010)

 

As another example, adding the negative addend 111110112 to 011010012 is equivalent to subtraction. There is a number in the eighth digit, meaning negative, so this number should be read as 2's complement.

 

) 011010012 (10510)

) 111110112 (=-510)

) 011001002 (10010)

 

It can be seen that the results of both calculations are equivalent to decimal numbers. It may seem strange whether it is necessary to add negative numbers without subtraction, but it is meaningful that you can do both addition and subtraction with only one calculator called an adder. In addition, the case where there are plural addend is as follows.

 

) 011010012 (10510)

) 000001012 (510)

) 100111002 (=-10010)

) 000010102 (1010)

 

In this way, when the result value is between 128 and 127 based on 8 binary digits, addition is calculated normally, but if it does, an error occurs as follows.

 

) 011010012 (10510)

) 001100102 (5010)

) 100110112 (=-10110)

 

You may need to expand the number of digits depending on the range of your results to avoid errors like this.

 

3. Subtraction operation

As opposed to addition, general subtraction reduces the number of the minuend. The following is a case of subtraction of 8 basic binary numbers, where the minuend is 011010012 and the subtrahend is 001100102, which is larger than the subtrahend.

 

) 011010012 (10510)

) 001100102 (5010)

) 001101112 (5510)

 

It can be seen that the result is the same even when converted to decimal number. The following is a case where the minuend is 001100102 and the subtrahend is 011010012, which is smaller than the subtrahend.

 

) 001100102 (5010)

) 011010012 (10510)

) 110010012 (=-5510)

 

The result of the operation has a number in the 8th digit, which is the sign, meaning that it is a negative number, so this result should be read as 2's complement. After converting 110010012 to 001101102 (1's complement), adding 1 to get 001101112 (2's complement) is converted to a decimal number and you can see that it is 5510.

 

4. Multiplication operation

It is a concept of repeating the addition operation, and adding only the multiplicand is repeated as much as the multiplier.

 

× ) 000100012 (1710)

× ) 000001012 (510)

× ) 00010001

× ) 0000000

× ) 010001   

× ) 010101012 (8510)

 

It can be seen that the result matches the decimal number. If the multiplier is negative, it becomes:

 

× ) 000100012 (1710)

× ) 111110112 (=-510)

× ) 00010001

× ) 0010001

× ) 000000

× ) 10001

× ) 0001

× ) 001

× ) 01

× ) 1           

× ) 101010112 (=-8510)

 

More than 8 digits results in out-of-range rounding, but the result is consistent with decimal multiplication.

 

5. Division operation

The opposite concept of multiplication, it is to find the number of times to repeatedly add a divisor until it becomes a dividend.

 

000001012 (510) ) 000000112 (310)

000001012 (510) ) 000100012 (1710)

000001012 (510) ) 0000101  

000001012 (510) ) 00000111

000001012 (510) ) 00000101

000001012 (510) ) 000000102 (210)

 

The result of the calculation matches the division of decimal numbers. You can see the quotient and remainder above and below the equation. In the case of division in which the divisor is negative, if only simple numbers are compared, the divisor becomes larger than the dividend, and when subtracting the quotient of each digit, the number rather increases. Therefore, the divisor is converted into 2's complement to calculate, and the obtained quotient is converted back to 2's complement.

 

Divisor: 111110112 (=-510) 00000101(510)

 

000001012 (510) ) 000000112 (310)

000001012 (510) ) 000100012 (1710)

000001012 (510) ) 0000101  

000001012 (510) ) 00000111

000001012 (510) ) 00000101

000001012 (510) ) 000000102 (210)

 

Quotient: 00000011(310) 11111101(310)

 

111110112 (=-510) ) 111111012 (=-310)

111110112 (=-510) ) 000100012 (1710)

111110112 (=-510) ) 00001111

111110112 (=-510) ) 000000102 (210)

 

If only one of the dividend and the divisor is negative, it is calculated as 2's complement and the quotient is converted back to 2's complement. If both the dividend and the divisor are negative, both are calculated using 2's complement and the quotient is obtained as it is.

 

6. Conclusion

Just know how to calculate this yourself (delete division part from your memory), and usually use the computer's calculator.

No comments:

Post a Comment