Notes on computer basics-original code, complement code, inverse code, frame code

Two's complement is a way of representing signed integers in computers, also known as two's complement. In two's complement notation, the complement of a positive number is the same as its original code, while the complement of a negative number is the inversion of its original code plus 1.

The specific complement representation method is as follows:

  1. For positive numbers:

    • The original code and the complement of a positive number are the same, that is, the original code and the complement are equal.
    • For example, for the decimal number +5, its original binary code is 00000101, and its complement is also 00000101.
  2. For negative numbers:

    • First, convert the absolute value of the negative number into the original code of the binary number.
    • Then invert the original code, that is, 0 becomes 1, 1 becomes 0, and the inverse code is obtained.
    • Finally, add 1 to the complement code to get the complement code.
    • For example, for the decimal number -5, its absolute value is 5, and the original binary code is 00000101. The inverse code is 11111010, and adding 1 is the complement code 11111011.

The advantage of the complement code is that it can simplify the addition and subtraction operations in the computer, so that the computer can use the same arithmetic circuit to handle the operations of positive and negative numbers, thus simplifying the hardware design. At the same time, the complement code can also avoid the zero symmetry problem of positive and negative numbers, making operations in computers simpler and more efficient.

Two's complement representation is the most common way to represent signed integers in computers, and almost all computer systems use two's complement representation to process integers. The widespread application of complement codes in computers makes operations on integers more convenient and efficient.

Applications of complement codes

Complementary codes are widely used in computers and are mainly used to represent signed integers and perform integer addition and subtraction operations. Here are a few major applications of complement codes:

  1. Representing signed integers: Two's complement can be used to represent positive and negative numbers. In computers, fixed-length binary numbers usually represent signed integers in two's complement, which simplifies the addition and subtraction of integers and avoids the zero symmetry problem of positive and negative numbers.

  2. Addition and subtraction operations: Addition and subtraction operations in computers usually use two's complement to represent and process integers. Complementary addition and subtraction operations can be implemented through simple bit operations, making computers more efficient when processing integer operations.

  3. Logical operations: In logical operations, one's complement is often used to represent signed logical values. For example, operations such as logical AND and logical OR can use complement representation to represent signed logical values ​​and perform logical operations through bit operations.

  4. Data Storage: In computers, one's complement is widely used to store signed integer data. Because two's complement representation simplifies the addition and subtraction of integers, using two's complement reduces computational complexity when storing integer data.

  5. Control signal: Complement code can also be used to represent control signals, such as conditional judgment, loop control, etc. in computers. The representation of complement codes can facilitate logical operations and conditional judgments.

In general, the complement code plays an important role in computers. It simplifies the representation and processing of signed integers, making computers more efficient and flexible when processing integer operations and logical operations. The application of complement codes allows computers to better handle integer data and plays an important role in control signals and logical operations.

Original code, complement code, inverse code, frame code

Original code, complement code, complement code and frame code are all coding methods used to represent signed integers in computers. They each have different characteristics and applications, and there is a certain relationship between them.

  1. Original code:

    • Features: The original code is the simplest representation method. It directly uses binary to represent integers. The highest bit is the sign bit (0 represents a positive number, 1 represents a negative number), and the remaining bits represent the absolute value of the integer.
    • Application: The original code is mainly used to understand the basic principles of binary representation, but it is rarely used in actual calculations because the addition and subtraction operations of the original code are more cumbersome.
  2. Inverse code:

    • Features: The inverse code improves the representation of negative numbers based on the original code. For negative numbers, the sign bit of the complement code remains 1, and the remaining bits are inverted (0 becomes 1, 1 becomes 0).
    • Application: The complement code is rarely used in computers and is mainly used to understand the calculation method of the complement code.
  3. Complement code:

    • Features: Two's complement is the most commonly used way to represent signed integers. For positive numbers, the complement is the same as the original code; for negative numbers, the complement is the complement plus 1.
    • Application: Complement code is widely used in computer arithmetic operations, including addition, subtraction, multiplication and division. Its main advantage is that it can simplify addition and subtraction operations in computers, and it can also represent the smallest negative numbers without repeated representation of positive and negative zeros.
  4. Frameshift:

    • Features: Frameshift is based on complement code and further improves the representation of negative numbers. For negative numbers, the sign bit of the frameshift is 0 and the remaining bits are inverted.
    • Application: Frameshifting is rarely used in computers and is mainly used to understand other representations of signed integers.

Relationship between each other:

  • One's complement and one's complement: For negative numbers, one's complement is the previous step before one's complement. The complement code is obtained by inverting the binary representation of the absolute value of a negative number bit by bit, while the complement code is based on the complement code and adding 1.
  • Original code and inverse code: The original code is the most basic representation method, and the inverse code is an improved negative number representation method based on the original code, that is, the binary representation of the absolute value of a negative number is inverted bit by bit.
  • Original code and complement code: Complement code is an improved representation method of negative numbers based on the original code, that is, the original code of the negative number is inverted bit by bit and 1 is added.

In general, the original code, complement code, complement code and frame code are different encoding methods used to represent signed integers in computers. The complement code is the most commonly used and is used for actual arithmetic operations. Understanding these coding methods can help us understand the representation and operation principles of integers in computers.

Guess you like

Origin blog.csdn.net/qq_40140808/article/details/131905443