Number of machines

Related concepts

true value

Numbers in line with human habits

Number of machines

The storage form of a number in the computer is a binary number . We call these binary numbers a machine number. The machine number is signed. In the computer, the highest bit of the machine number is used to store the sign bit. 0 means a positive number and 1 means a negative number.

the truth value of the machine number

Because it has a sign bit, the formal value of the machine number is not equal to its true value. Taking the machine number 1000 0111 as an example, its real value is -7, while the formal value is 135. The value of the true representation of a signed machine number is called the truth value of the machine number.

original code

The expression of the original code is the same as that of the true value of the machine number, that is, the first bit is used to represent the symbol, and the remaining bits are used to represent the value. For example, the plus or minus 1 of the decimal system is represented by the original code of 8-bit binary as follows:

inverse code

The representation method of inverse code is:

  • The one's complement of a positive number is its original code itself.
  • The one's complement of a negative number is based on the original code, the sign bit remains unchanged, and the rest of the bits are reversed .

$[1] reverse=[1] original \ [-1]_ reverse=(11111110)_2$

Complementary code (the storage form of data in the computer)

Adding one's complement is just a property of complement code, which cannot be defined as complement code. The complement of a negative number is a binary code that can be added to its opposite number and overflow so that the calculation result in the computer becomes 0 (that is, the opposite number in the binary sense).

Quoted from Universal Netizen

The encoding method of complement code is:

  • The complement of a positive number is the original code itself.
  • The complement of a negative number is based on its complement, plus 1 .

$[1] complement=[1] original \ [-1] complement=[-1] reverse+1=(11111111)_2$

design concept

Why use two's complement as the form of data storage in computers

1. Guarantee the uniqueness of 0

原码和反码,0 都有 +0 和 -0 两种编码,这违背了编码唯一性的原则[1]
但是对于补码来说,+0 和 -0 均指向 $(00000000)_2$[2]

2.能多出来 1 个数字

在上一条中已经提出了,0 被唯一指向为 $(00000000)_2$ ,那么就多出了一种指向 $(10000000)_2$[3]可以用来表示 -128

3.能正确进行负数运算

原码和反码的运算缺陷

原码:$1+(-1)=(00000001)_2+(10000001)_2=(10000010)_2=-2$

反码: $(+0)+(-0)=(00000000)_2+(11111111)_2=(11111111)_2=-0$

由上述例子可见原码和反码在减法上均有问题

补码的诞生

此时为了计算机在底层能尽可能的减少分支判断,补码就被设计出来了。

下面就补码运算的正确性进行验证:

首先来看下大家生活中常见的时钟,我们发现时钟顺时针转和逆时针转似乎能达到一样的效果。那么我们可以看出在时钟中 +10 和 -2 是等价的。(仅仅在时钟上看的时针、分针和秒针)

那么在二进制中是否能进行类似的操作,把负数变换成一个等价的正数。在此之前我们需要引入 同余的概念(就是上面时钟的原理)

可以看见 13 和 1是 同余的,而 13=10+3,1=-2+3 ,因此可看出可以把 -2 变成 +10

引用上面的方法,我们可以把二进制的负数进行变化,变化为对应的正数,从而实现把减法变加法的操作。

下面开始讲述如何进行把二进制的负数进行变化

Add one more point at this time: 8-digit binary numbers are used as the basic unit in computers, and numbers with more than eight digits will only retain the last eight digits, and the previous digits will be discarded naturally . This is actually the idea of ​​taking the remainder.

From $\ -x\equiv mx \ (mod\ m)$, it can be seen that the binary code of negative numbers $=(100000000)_2-$ original code. This binary code is the complement

Now the operation of negative numbers only needs to be converted into complement code: the addition operation can be performed. This is the definition of complement code : the complement code of a negative number is a binary code that can be added to its opposite number and overflow to make the calculation result in the computer become 0 . (Inverse code plus one is just a property of complement code, which cannot be defined as complement code.)

Complement weights


  1. 1. The encoding needs to ensure its uniqueness, so as to ensure the accuracy of information transmission.
  2. 2. Theoretically, -0 should be 100000000, but the computer uses 8 bits as a bit, which means that there can only be 8 bits at most, and the extra 1 will naturally be overflowed. If you don't understand, please point it out↩
  3. 3. -128 is special, it only has original code and complement code, no inverse code.

Guess you like

Origin blog.csdn.net/qq_40790680/article/details/128971617