[Principles of computer composition] Original code inverse code and frame code

Table of contents

1. The concepts of original code, complement code, complement code and frame code

1. True value 

2. Number of machines

1. Original code

2. Inverse code 

3. Complement code

4. Frameshift 

2. Conversion of original code, complement code, complement code and frame code 

1. Convert original code to reverse code

2. Convert original code to complement code 

3. Original code transfer code 

3. Summary 

Attachment: Related content


1. The concepts of original code, complement code, complement code and frame code

1. True value 

Binary numbers are positive and negative just like decimal numbers. When writing, you can use "+" and "-" to represent the symbols of data. This writing format is called true value.

For example: +3 and -5 in decimal, +011 and -101 in binary are all true values.   

2. Number of machines

Since data has only two symbols, positive and negative, it is natural to use binary 0 and 1 to represent the symbol of data in computers. The binary number represented by the symbol and numerical value is called a machine number or machine code. Commonly used machine numbers include original code, complement code, complement code and frame code. 

For example: (The machine numbers here are all original codes) The first bit of 1,0001 represents the sign bit, and 1 represents a negative number. The comma separates the sign bit from the numerical bit. What follows the comma is the numerical bit. 0001 is binary, so it is converted to After decimal, the true value is -1;

For another example, the true value of the decimal number represented by 0,101 is +5. 

1. Original code

The original code is a symbolic value, and its encoding rules are simple and intuitive: the sign bit of positive numbers is represented by 0, the sign bit of negative numbers is represented by 1, and the numerical bit remains unchanged.

For example:

x=+0.1101, then [x]original=0.1101; x=+1101, then [x]original=01101

x= -0.1111, then [x]original=1.1111; x= -1111, then [x]original=11111 

The original code data representation is simple and intuitive, just add the sign bit to the absolute value of the binary number. However, there are two machine 0s in the original code, which will cause trouble in data operations. In addition, the addition and subtraction operations of the original code are complicated, and the sign bit cannot directly participate in the operation. The addition operation requires "summing with the same sign and finding the difference with different signs". The subtraction operation requires "summing with the same number and finding the difference with the same sign". When finding the difference, you need to compare the size first, then subtract the decimal from the large number, and the sign of the final result The choice is also relatively complicated. Obviously, it is inconvenient to use the original code as a machine number to implement addition and subtraction operations. The original code is currently only used to represent the tail code of floating point numbers in computers. 

2. Inverse code 

The complement code is also called the complement code of 1. Its sign bit is the same as the original code. When the true value is a positive number, the complement code is the same as the original code; when the true value is a negative number, the numerical bit of the complement code is the inversion of the true value bit. 

For example:

x=+0.1101, then [x] inverse=0.1101; x=+1101, then [x] inverse=01101 

x= -0.1111, then [x] inverse=1.0000; x= -1111, then [x] inverse=10000

The sign bit of the complement code is the same as the original code. When the true value is a negative number, the numerical bits need to be inverted bit by bit. Similarly, there are two zeros, +0 and -0, in the complement code. The addition and subtraction operations of the complement code are slightly simpler than the original code. The sign bit can be directly involved in the operation. The addition operation can directly add the complement codes, but the carry of the highest bit is required. Add from the lowest bit of the operation result (carry round). The subtraction operation only requires adding the complement of the minus of the minuend to the minus of the negative minus of the minuend, and a circular carry operation method is also used. But despite this, modern computers do not use complement codes for data representation and operations. This is because people have found a better encoding - complement codes.

3. Complement code

The binary data in the computer has a word length limit. The bit weight of the highest carry bit of the data is the modulus. The part of the operation result that exceeds the modulus will be automatically discarded. Therefore, the operation of computer binary data is a typical modular operation, which is very Suitable for representation and operations using complement codes. 

For example:

x=+0.0101, then [x] complement=0.0101;

x= -0.0101, then [x] complement=1.1011;

x= -0.0000, then [x] complement=0.0000;

x= -1.0000, then [x] complement=1.0000;

The representation of the complement code is more complex than the original code, but it only has a unique 0. The sign bit can directly participate in the operation. The carry of the sign bit will be automatically discarded as a modulus during the operation. Its unique representation method allows the subtraction operation to be converted into an addition operation. It greatly facilitates binary operations. At present, computers generally use two's complement to represent signed integers. 

4. Frameshift 

Frameshift is only used for the representation of fixed-point integers, and is usually used to represent the exponent of floating-point numbers. The encoding method is to directly add a constant offset to the true value x.

For example:

x=+1010110, then [x] shift=11010110;

x= -1010110, then [x] shift=00101010; 

Frameshift has the following characteristics:

① In the sign bit of the frameshift, 0 represents a negative number and 1 represents a positive number;

② The frameshift and complement of the same value are the same except for the opposite sign bit;

③ The representation of 0 in frameshifting is also unique, specifically expressed as 100000…. 

2. Conversion of original code, complement code, complement code and frame code 

1. Convert original code to reverse code

When the true value of the original code is a positive number, the machine number of the complemented code is equal to the machine number of the original code.

When the true value of the original code is a negative number, the machine number of the complement code is equal to the inversion of the machine number of the original code (the sign bit remains unchanged)

For example:

x=+0.1101, then [x] inverse=0.1101; x=+1101, then [x] inverse=01101 

x= -0.1111, then [x] inverse=1.0000; x= -1111, then [x] inverse=10000

2. Convert original code to complement code 

When the true value of the original code is a positive number, the machine number of the complement code is equal to the machine number of the original code.

When the true value of the original code is a negative number, the machine number of the complement is equal to the inverse of the machine number of the original code plus 1 (the sign bit remains unchanged) 

For example:

x=+0.0101, then [x] complement=0.0101;

x= -0.0101, then [x] complement=1.1011;

x= -0.0000, then [x] complement=0.0000;

x= -1.0000, then [x] complement=1.0000;

To put it simply, the complement of the original code plus 1 is the complement. 

3. Original code transfer code 

When the true value of the original code is a positive number, the machine number of the frameshift is equal to the original code, but the sign bit needs to be changed.

When the true value of the original code is a negative number, the machine number of the frameshift is equal to the inverse of the machine number of the original code plus 1 (the sign bit is inverted) 

For example:

x=+1010110, then [x] shift=11010110;

x= -1010110, then [x] shift=00101010; 

To put it simply, the complement value of the original code remains unchanged, and the sign bit is inverted, which is a frameshift.

3. Summary 

① Original code, complement code, complement code, and frame code are all representation methods of machine numbers. In fact, they are converted into numbers (true values) understood by humans in the same way. However, human habitual thinking treats complement code, complement code, and frame number. Framecode understanding is more difficult. Generally we need to convert it to the original code and then convert it to decimal for understanding. 

For example: 1,00010 (1 represents the sign bit, 00010 represents the numerical bit) is the complement of a machine number. If we want to know its true value, we need to convert it to the original code first. Its original code is 1,11110. The sign bit is 1, indicating that it is a negative number, and the numerical bit is 11110, which is binary. When converted to decimal, it is 30, so the true value of the machine number is -30. 

② When the true value is a positive number, the complement and complement are equal to the original code, the frame bit is equal to the original code, and the sign bit is inverted. 

③ The zeros represented by original code and complemented machine numbers are positive and negative; there is only one zero represented by complemented and framecoded machine numbers (that is, positive zero and negative zero are equal). 

Attachment: Related content

serial number related information direct link
1 Original code inverse code frame code https://want595.blog.csdn.net/article/details/127607415
2 Fixed point and floating point numbers https://want595.blog.csdn.net/article/details/127625409
3 Fixed point addition and subtraction operations https://want595.blog.csdn.net/article/details/127681821

Guess you like

Origin blog.csdn.net/m0_68111267/article/details/127607415