### Original, complement, inverse

These three are all methods of representing the binary of numbers in a computer.

Byte-type data, occupies 8 bits, then its original code, inverse code, complement code are all 8 bits. Byte-type data, which occupies only 8 bits, can only represent 0 to 255.

Word-type data, which occupies 16 bits, then its original, inverse, and complement are all 16 bits. Word data, which occupies 16 bits, can represent 0 to 65535.

Negative numbers exist as complements in computers!

The regulation of the three:

One, the positive integer’s original code, inverse code, complement code is exactly the same, that is, the sign bit is fixed to 0, the numerical value bit is the same

Two, the negative integer’s sign bit is fixed to 1, from the original code into the inverse code, complement code, the rules are as follows:

<fontcolor=defaultsize=4>To To find the original, complement, or inverse code of a number, the following steps are required</font>

### What is the difference between the original code inverse and complement?

Original code is the sign bit plus the absolute value of the true value, i.e., the first bit is used to represent the sign and the remaining bits represent the value.

The inverse of a positive number is itself, while the inverse of a negative number is based on its original code, with the sign bit unchanged and the remaining bits inverted.

The complement of a positive number is itself, the complement of a negative number is based on its original code, the sign bit remains unchanged, the rest of the bits are inverted, and finally +1 (i.e., +1 on top of the inverse code).

The original, inverse, and complement of a positive number are all the same.

Trueform is a binary fixed-point representation of a number in a computer.

Trueform representation in front of the value of an additional sign bit (that is, the highest bit for the sign bit): the positive number of the bit for 0, the negative number of the bit for 1 (0 there are two kinds of representation: +0 and -0), the rest of the bit indicates the size of the value.

### Original code, inverse code, complementary code, shift code

Written in front of: the article for my study of some of the notes and insights, published mainly to record their own learning process. I’m not very knowledgeable, so my notes will inevitably be insufficient or even incorrect, but I will update them from time to time.

Basics: Suppose there is an n-bit binary number

This binary number has a total of state, the maximum number of

Reverse, written in binary for 10000000, a total of 8-bit, 1 after the 7 decimal

The following examples are n-bit number, the example of the 8-bit number

Original code

Simple Straightforward binary, the following examples are for fixed point numbers.

Fixed-point pure decimal: 01000000 the first bit for the sign bit, 0 for positive 1 for negative, here represents 0.1 (10)

Fixed-point pure integer: 00000001 here represents 1 (10)

Because there is a sign bit, so there are positive and negative zeros 00000000 and 10000000

Data range: – 127~127 (the last 7 bits are all 1)//equation is expressed as

Features: the original code is not suitable for addition and subtraction, but suitable for multiplication and division

Inverse code

The inverse code of a positive number is the same as its original code; the inverse code of a negative number is the inversion of the original code bit by bit after the sign bit, and the sign bit remains unchanged (1)

Range of data that can be expressed by the inverse code: same as the source code

Inverse code can be expressed by the inverse code: same as the source code

Inverse code can be expressed as a range of data.

Complementary code

Purpose: to facilitate the computer to add or subtract

Characteristics: suitable for adding or subtracting digital representation in the machine

Complementary code to achieve the computer, “plus the negative number of the essence of the principle of modal operations, that is, A minus B is equal to the complement of A plus B with respect to A and then modulo. As if the clock clockwise dial 3h and counterclockwise dial 9h to get the same result.

Binary complement:

Complement = (original + modulus) (mod modulus), it is clear that if the original code is positive, the complement is itself, for positive numbers do not have to consider the complement at all.

For computers, since two summed numbers have the same number of digits (n) and the sum can’t exceed n+1 digits, the modulus that should be taken is 100000… (n zeros).

So for n-bit pure decimals, its modulus (in decimal) is 2, and for n-bit pure integers, its modulus is 2n

Modulus: (100000000)

Original: (00000000)

Notice that even though the sign bit doesn’t have any numeric information, the reason that modulo takes the sign bit into account here is that we can define the complement of the first sign bit to be involved in the computer’s calculations to get the desired result.

(At the same time, taking the sign bit into account allows us to get the complement directly from the result when solving for the binary complement using mathematical formulas

Example: x=-0.1011

[x]complement=10+x=10.0000-0.1011=1.0101

Originally, it was meant to be 0.0101(2), but it just so happened that the first 1 could represent the original number, which was 0.0101(2), and the first 1 could represent the original number. (But it happens that the first 1 can indicate the negative sign of the original number, so you can directly read the complement of 10101

)

So for the complement, the sign bit plays a role in indicating the positive and negative signs, but also involved in arithmetic.

Also, unlike the original code, which has two zeros (positive and negative zeros), in the complementary code provision, there is only one zero (00000…). (the positive 0, since the original code is also all 0s), while 10000… Can be expressed -1 (complementary pure decimal) or -2n-1 (complementary pure integer)

// Can be remembered this way (to pure integer as an example): because the back n-1 zeros after the inverse of n-1 for n-1 1, plus 1 for 2n-1 (10), the front a 1 indicates a negative number, so the complementary code can be expressed -2n-1

complementary code how to come: the original code is positive, the complementary code is the same as the original code; The original code is negative, the number of bits after the original code for the inverse plus 1

Shift code

Purpose: in order to facilitate the computer than the size of the elimination of the sign bit of the computer interference

Principle is the negative part of the whole to the direction of the non-negative numbers, that is to say, to the first bit of the sign of the sign of the sign of the elimination of the significance of the first bit. Elimination method: for the complement of positive numbers, the sign bit from 0 to 1, increase; for the complement of negative numbers, the concept of the sign bit elimination, in the computer is defined as a positive number, but also in order to ensure that the original negative number is smaller than the original positive number, the sign bit from 1 to 0.

In order to ensure that the size of each number of the relationship remains unchanged between the complement of the code to be converted into a shifted code, with the original code to be converted, if the size of the negative number of relationships between the numbers will be be reversed.

Mathematical formula:

Macroscopically it is a translation of the entire centered axis to the non-negative half-axis, and the size relationship between each number remains unchanged.

Pure decimals [X] shift = 1+X

Pure integers [X] shift = (general standard)

How does the shift code come about: the shift code has the same number of tails as the complement code, with the sign bites reversed (i.e., 1->0;0->1 in the first place of the complement code)

Because the shift code comes from the complement code, it can also represent one extra number