Computer Composition Principle Notes - Source Code Inverse Complement and Its Calculation

Principles of Computer Composition-Study Notes (for personal use, updated from time to time)_گThis is a blog with a QQ name-CSDN Blog

Source code complement addition and subtraction

This content is more extracted from the general chapter and recorded separately:

First understand what is source code:

Before that learn about truth values ​​and machine numbers:

True value: a binary number that can be correctly calculated (other bases are also available, generally use decimal), which can correctly represent the size of a number, and the +- sign is required to indicate the size

For example -1

Machine number: (fixed-point number, floating-point number) source code is a kind of fixed-point number representation, the computer can correctly identify the positive and negative numbers, and the positive and negative in the computer need to use 0, 1 at the beginning

For example, 1000 0001 (assuming the word length is 8bit)

For example, a string of source codes has (two positive numbers): A=0000 1110=14, B=0000 0001=1

Now add the two numbers to get C=0000 1111=15

Now subtract the two numbers to get C=0000 1101=13

It can be seen that if the addition and subtraction of two positive numbers does not exceed the limit, its value is correct

But now I change B to 1000 0001, is the result correct after recalculation?

Calculate=A+B=0000 1110+1000 0001=1000 1111=-15

You will find that we cannot add a negative number directly with the source code , that is to say, we cannot directly add and subtract a negative number with the source code (but two positive numbers seem to be ok, then we directly change the number to a positive number, and also Just change 1 to 0, + sign to minus sign)

If this is the case, isn't it okay for us to convert negative numbers into positive numbers and then use subtraction?

It is a pity that the computer will not add a subtraction operator to the computer due to the manufacturing cost, only an addition operator

It's scary at this time. In order to save money, scientists have invented a method that can use addition instead of subtraction.

Now look at the calculation formula: 0000 1110+1000 0001=1000 1111=-15 Isn’t this a wrong result (source code calculation)

Now change it to: 0000 1110 + 1111 1111 =0000 1101=13 But you will find that it is magically correct (complement arithmetic)

You may ask what is this so awesome?

That's right, this thing is the complement code. There may be errors when adding two source codes directly, but there is no problem when adding two complement codes

So what is the complement:

Imagine that you have a clock now, with 12 as a reference, then in a sense, the value expressed by 9 relative to 12 and -3 relative to 12 is the same, (simple understanding of the complement is similar to -3 and 9 here , compared with a benchmark value, the number can be replaced, that is, I couldn’t calculate it with 9-3, but now it is possible to calculate it by changing it to 9+21 = 30, 30 is equal to 12 = 6 )

How to calculate the complement: (Positive numbers remain unchanged) Negative numbers: Remove the sign bit from the source code to get other numbers from 0 to 1, 1 to 0, and add 1 to the end of the final number. Don’t forget to consider the carry (the same is true for converting the complement code into the source code, first change the number at +1)

Anyway, the final result is like this, now you can directly use addition to express the addition and subtraction of positive and negative numbers

Hey, didn’t you say you can’t use subtraction? Yes, we don’t have a subtraction operator, but we can change a source code into the opposite source code, that is, add a negative sign in front and get its complement

That is to say , for example : A=0000 1110=14; B=1000 0001=-1

Now calculate the value of [AB]

That is A+-B=0000 1110 source +0000 0001 source

It needs to be converted into complement code: 0000 1110+0000 0001=0000 1111=15

For example, A=0000 1110=14; B=0000 0001=1

[AB] =00001110+1000 0001 (source code)

Conversion complement=0000 1110 +1111 1111=0000 1111=15

At this time, some people may wonder why the sign bit can also participate in the calculation: in a sense, there is no sign bit at all, and the complement is just converting a negative number into a positive number based on the reference value. Because we have pointed out above that there is no problem in adding two positive numbers, no matter how we transform it, it will be the result of directly adding the two positive numbers, which is why the complement of the positive number is still the original result. Value, because it does not need to be changed, it can be directly involved in the calculation, but the negative number is different, when we directly add the negative number, there will be an error, so we need to change it to complement, that is, to change it relative to the reference value positive number

For example, 9 is still 9 compared to 12, are you sure you need to change it once, and -3 is different, you need to convert it to 9, and add it to 9 to get 18, so 9+-3=6 is equivalent to 18 is equal to 6 relative to 12

 Well, the principle of complement code is here for the time being, and here are some techniques for transcoding with complement code: Generally, what is needed is to change the complement code into the opposite number so that we can directly calculate the subtraction, such as [AB] complement, generally directly give B to get value, we need the value of -B to calculate

It is [B] complement to quickly convert [-B] complement, you can directly convert B complement into source code, change the sign bit, and then convert to complement,

The second is to find the first 1 from right to left on the basis of [B] complement, the right half remains unchanged, and the left half is reversed

Assuming known B complement = 1110 1000, calculate -B complement

B complement=1110 1 000 , conversion: 0001 1 000

Addition and subtraction overflow judgment 

 

 

 

Multiplication and division (source code complement multiplication and division is also quite troublesome, and I will have time to record the details in the future)

 Source code multiplication

 

 

 two's complement multiplication

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_55042292/article/details/126007398