Bits obey orders: The generation process of complement codes and the storage of integers in computers

Article directory

  • refer to
  • environment
  • statement
      • Eight-bit binary adder
      • Octal value
  • Original code
      • concept
      • Positive and negative numbers and zero
      • Operations with positive and negative numbers
  • reverse code
      • concept
      • a step far
  • complement
      • concept
      • the only zero
      • Addition
  • Advantages and Disadvantages of Complement Code
      • advantage
          • Simplify arithmetic operations
          • Identify positivity and negativity at a glance
          • There is no need to process the sign bit during the operation.
      • shortcoming
          • Not easy to read

Reference

Project Description
Search Engine Bing, Google
AI large model Wen Xinyiyan< /strong>, Tongyi Qianwen, iFlytek Spark Cognitive Model, ChatGPT
Bilibili Watch too many videos

Environment

Project Description
PHP 5.5.0, 5.6.8, 7.0.0, 7.2.5, 7.4.9 , 8.0.0, 8.2.9
PHP Editor PhpStorm 2023.1.1 (Professional Edition)
Drawing Tools draw.io 21.7.5

Statement

Eight-bit binary adder

An eight-bit binary adder is an electronic circuit used to perform the addition operation of two eight-bit binary numbers. This adder can add two eight-bit binary numbers to produce an eight-bit binary sum. For this, please refer to the following example:

 A: 1 0 1 1 0 1 0 1
   B: 0 1 1 0 1 0 1 1
-----------------------
Sum: 1 1 0 1 1 1 0 0

In this example, each bit is added following standard binary addition rules, including carry. If the sum of the two bits added is greater than 1, then a carry will occur, and this carry will be added to the operation of the next bit. If a carry would be applied to the ninth binary digit, the carry will be discarded.

Octal value

In this article, we will simulate an eight-bit binary adder for addition operations. Therefore, expressing an integer as an eight-digit binary number does not mean that using the original code, one’s complement and one’s complement can only be used to convert an integer into an eight-digit number. Bit Binary number.

Original code

Concept

The original code is a binary representation of integers, also known as symbol-numeric representation. The original code is mainly used to represent signed integers (positive numbers, negative numbers and zero) and is widely used in computer systems.

In the original code representation of binary numbers, binary numbers are divided into two parts, namely sign bit and numeric specific value.

  1. sign bit
    The highest bit of the original code is the sign bit. The sign bit is used to represent the positiveness of a value, 0 represents a positive number, and 1 represents a negative number.
  2. accurate value
    The binary digits after the sign bit are used to represent specific values.

Give me a chestnut

The value 2 is represented as 0000010 using a seven-digit binary number. If you need to express the positiveness of a value, add a binary digit before the highest bit of the binary number. If the binary bit stores the number 1, then the binary value represents the decimal number -2; if the binary bit stores the number 0, then the binary value The numerical value represents the decimal number 2.

Positive and negative numbers and zero

Zero is neither a positive number nor a negative number. There is a flaw in using the original code to represent numerical values, that is, zero has positive and negative properties, and there are positive zero and negative zero.
In the original code representation, since we directly use the highest bit of the binary number as the sign bit, when all other binary bits are 0, we will get the value zero Two different representations, namely Positive zero and Negative zero. This is obviously unreasonable. The redundancy of representing zero is one of the main flaws of the original code, and one of the reasons why computers do not use the original code to store integers.

Operations of positive and negative numbers

The original code can represent positive and negative numbers, but there will be problems when adding positive and negative numbers. For this, please refer to the following example:

It is known that the binary representation of + 2 is 00000010, and the binary representation of -2 is 10000010. Add the two,

00000010
10000010
----------
10000100

The result obtained is 10000100, which converted to decimal number is -4. Adding + 2 to -2 should result in zero, but instead we get -4. The original code can represent positive numbers, negative numbers and zero, but cannot correctly handle the addition of positive and negative numbers. This is another reason why computers do not use the original code to store integers .

Inverse code

Concept

The complement code is the same as the original code and is used for binary representation of signed decimal integers. The complement code is based on the original code. For positive numbers and negative numbers, the ways to obtain the complement code are as follows:

  1. The complement of a positive number is its original code.
  2. The complement code of a negative number is obtained by inverting the binary bits in the original code except the sign bit bit by bit (each binary number changes from 0 to 1 or from 1 to 0).

Give me a chestnut

 The complement of + 5 is expressed as: 00000101
The original code of -5 is expressed as: 10000101
The complement of -5 is expressed as: 11111010

One step away

The complement code has the same shortcomings as the original code. There are two ways to represent zero, namely positive zero and negative zero. The complement of + 0 is represented as 00000000, and -0 is represented as 11111111.


The complement code can implement positive numbers and negative numbers or addition between negative numbers and negative numbers. For this, please refer to the following example:

Give me a chestnut

 The complement of + 3 is expressed as: 00000011
The complement of -5 is expressed as: 11111010
Add the two:
00000011
11111010
----------
11111101

The obtained 11111101 is the inverse code representation of the result, and we need to perform reverse code operation on it. Try to invert the binary bits except the sign bit bitwise, you can get 10000010, convert it into a decimal value, the result is -2.

Complement code

Concept

In computers, integers are usually stored using their complement representation. The complement code is the same as the complement code and the original code, and are used for the binary representation of signed decimal integers. The complement code is based on the complement code. For positive numbers and negative numbers, the ways to obtain the complement code are as follows:

  1. The complement, original code and complement code of a positive number are all the same.
  2. The complement of a negative number is one's complement + 1.

The only zero

In the two’s complement representation of a signed decimal integer, there is only one way to represent zero, which is 00000000.
By performing the operation + 1 on the complement of a negative number, the complement representation of -0 changes from 11111111 to 100000000, the ninth binary number 1 was abandoned because there was no place to put it. So 0 only has the representation of 00000000.

Addition operation

After performing addition operation between complements, the result needs to be subjected to inverse complement operation to obtain a binary representation that is easy for humans to understand, so as to facilitate the conversion of the result into Decimal value. For this, please refer to the following example:

 + 3’s complement representation is: 00000011
The complement representation of -5 is: 11111011
Add the two:
00000011
11111011
----------
11111110

The resulting 11111110 is the two’s complement representation of the result. In order to convert it into a correct decimal number, we need to perform the inverse complement operation on it to obtain its original code representation. First, perform -1 processing on 11111110 to obtain 11111101, and then perform bitwise inversion operation on the binary bits except the sign bit gets 10000010 and converts it into decimal number to get the final result -2.

Advantages and disadvantages of complement codes

Advantages

Simplify arithmetic operations

By converting integers to their two’s complement representation, addition and subtraction operations can be processed using the same hardware circuitry. When subtracting two positive integers, you only need to understand the subtrahend as a negative number and then add it to the minuend. For this, please refer to the following example:

10 - 5 = ?
The above expression can be understood as:
10 + (-5) = ?
Simply think of the subtrahend 5 as (-5) to convert a subtraction operation into an addition operation.

Using the same hardware circuit for addition and subtraction processing can bring the following advantages:

  1. Hardware simplification
    When the same circuits are used for addition and subtraction, the number of hardware components required in the computer can be reduce, which reduces the cost and complexity of the system.

  1. save space
    Since only a general-purpose adding circuit is required, the physical size of the computer can be smaller. This is important for embedded systems and mobile devices as they often have limited space.
  2. Reduce power consumption
    Using the same hardware circuitry to perform addition and subtraction operations can reduce power consumption. This is especially important for battery-powered systems such as mobile devices, because reducing power consumption can extend battery life and increase device endurance.
  3. Improve performance
    Using the same hardware circuit can improve computer performance because the circuit can be highly optimized and parallelized to speed up arithmetic operations.
Identify positive and negative at a glance

Since the highest bit of in the complement code is the sign bit, it represents the positive and negative nature of the value. Therefore, you only need to observe whether the highest bit of the complement code stores the binary number 1 or 0, and you can determine the positivity of the value.

There is no need to process the sign bit during the operation

When performing two’s complement operations with different or the same (positive or negative) sign, no need to perform special processing on the sign bit, the sign bit and other binary bits in the complement can be Perform addition or subtraction together.

Disadvantages

Difficult to read

After performing the two’s complement operation of addition or subtraction, the result is its two's complement representation. If you need to convert it into a decimal number yourself, you often need to first go through inverse complement operation, that is, restore the complement to the original code. After getting the original code, you can easily convert it into a decimal number.