search
HomeCommon ProblemWhy are negative numbers in computers stored in two's complement?

The use of two's complement storage for negative numbers in computers can simplify the basic computer arithmetic circuits, so that addition and subtraction only need to be implemented with addition circuits, and addition is used instead of subtraction. The complement is the smallest positive congruent remainder of a negative number, so adding a negative number and subtracting a positive number can both be represented by adding a complement.

Why are negative numbers in computers stored in two's complement?

The operating environment of this tutorial: Windows 7 system, Dell G3 computer.

1. Introduction

Do you know how integers are stored in computers? Is it the sign bit plus the value bit? Are the value bits stored in normal binary format?

If you answer yes to the last two questions, it means that when stored in 3-bit binary and the sign bit 0 represents positive and 1 represents negative, 1 will be stored as 001 , -1 will be stored as 101. Unfortunately, this is not the case. Computers store integers in the form of two's complement instead of the natural-looking form just now. Although the two's complement is also represented by the sign bit plus the value bit, the rules of expression are different: 1 will Save as 001, -1 will be saved as 111.

If you answered all three questions correctly, you know that integers are stored in two's complement format in computers, but do you know why this format is used? And "The complement of a positive number is equal to the original code; the complement of a negative number is equal to the complement plus 1, and the complement is equal to the sign bit of the original code unchanged, and the remaining bits are inverted." What does such a complement mean? What? (Assuming you don’t know, please read on XD)

Let’s first look at the purpose of using complement codes, and then forget about the definition of complement codes above. Start with this purpose and explore the essence of complement codes step by step. .
Purpose: In order to simplify the basic computer operation circuit, addition and subtraction only need to be implemented through the addition circuit, that is, subtract a positive number or add a negative number Such operations can be used Add a positive number instead. So the storage form of negative numbers is changed and stored in a form that can be directly added as positive numbers. This form is the complement code. (Positive numbers do not need to be changed, so positive numbers are generally omitted in the following discussion)

2. How does the complement code turn subtraction into addition?

2.1. Use the clock to understand subtraction to addition

This is an example around you. When you proofread the clock, suppose You find that the clock is at 6 o'clock, but it is actually only 2 o'clock now, that is, it is running 4 hours faster. You can correct it in two ways, one is to turn it back 4 hours counterclockwise to 2 o'clock, and the other is to set the clock back 4 hours to 2 o'clock. It is to dial 6 hours clockwise to 12 o'clock and then dial 2 hours, that is, dial 8 hours clockwise to 2 o'clock. So for the clock dial, suppose -N means turning N hours counterclockwise, N means turning N hours clockwise, then -4 = ​​8, there will also be -1 = 11, -5 = 7, and even -4 = ​​8 = 20 = 32 = -16. ..

What rules are hidden here? In fact, in mathematics, -4, 8, 20, 32, -16 can be classified as the same type of numbers that meet certain conditions - for modulo 12 congruence.

The definition of modulus and congruence on the Chinese Wiki is: two integers a and b, if the remainders obtained by dividing them by a positive integer m are equal, then a and b are said to be congruent modulo m. .

In an overflowable counting system, if the capacity of the counting system is taken as the modulus, then all numbers that are congruent to this modulus will have the same representation in this counting system, and the operations will be equivalent.
For example, the clock dial in the above example is an overflow counting system, the modulo is 12, so -4, 8, 20, 32, -16These numbers that are congruent to modulo 12 are on the clock dial The expressions above are the same, and the results of these operations on the hour hand are also the same, they will all be set to the same position.

A counting system composed of n-bit binary will discard the overflowing high bits, so it is also an overflowable counting system, and its modulus is \(2^n\). (Counting from 0 to \(2^n -1\), any more will overflow)
It can be inferred that in a counting system composed of 3-bit binary modulo 8, -2, - 10, 6, 14 can be represented by the same binary number. Subtracting 10 and adding 14 at the same time will get the same result.

2.2. Deriving the complement code

So, as long as the complement code is the positive congruent remainder of a negative number, then addition can be achieved This positive congruence complement has the same result as adding another negative number. For a negative number, there are countless positive congruences that meet the conditions. In order to reduce unnecessary operations, it can be specified that the complement is the smallest positive number among them.

Maybe because finding the complement code through the original code is a complementary modulo operation, it is called the complement code.

Note that the complement codes here are all marked by me with special marks, because this is not the real complement form stored in the computer. It should be called the complement number, but believe me, it is very close.

3. Improve the complement code

3.1. There are still some problems with this complement code representation

By converting to two's complement, subtracting a number does become adding a number. It seems very good, but there is an obvious problem, that is, the sign of the number itself is lost.
For example, 3-digit binary normally represents 0~7. Using the complement method, it can replace the operation of -8~-1, but it cannot truly represent - 8~-1, because you don't know whether it is a positive number or a negative number.
We converted negative numbers into a form that is more palatable to computers in calculations, but it lost its own information as a number.

How to solve this problem? Some people may quickly slap their heads: Then add one digit to indicate positive or negative. But what should I do when calculating in this case? Should I start counting from the second digit? When carrying out bits, do we need to pay special attention not to affect the sign bit? You will find that the problem is not that simple.

3.2. How to improve the complement code

I don’t know how Daniel came up with the idea, but the problem was solved perfectly:

  • Under the premise of maintaining the characteristics of the complement code (that is, subtracting a number still becomes adding a number)
  • Increase the expression of positive and negative (can truly represent -8~-1, just look at whether the sign bit is 0 or 1)
  • can also make the operation without distinguishing the sign bit, directly treating the sign bit as the value bit for calculation, and the positive and negative sign of the result It will naturally conform to this positive and negative representation (that is, the carry of the sign bit and the carry of the value bit will be naturally reasonable)

And the solution is really skinny, surprisingly simple, that is, the previous The idea you thought of: Add one digit to indicate positive and negative.
The specific method is: Add a sign bit to the high position on the left. This sign bit, together with the pseudo-complement code we deduced earlier, forms a truly perfect complement code.
Achieved effect: By reading the sign bit, you can know the sign of the number. At the same time, the sign bit participates in the operation, carry, and carry out like the value bit in the addition operation.

4. Finally

summarize

  • The purpose of using complement code: to simplify the basic computer operation circuit, So that addition and subtraction only need to be implemented with an addition circuit, and addition is used instead of subtraction.
  • Why the complement code can achieve this purpose: n-bit binary can form an overflowable counting system. In such a system, the capacity of the counting system is used as the modulo, and all modulo are congruential to this The numbers will have the same representation in this counting system, and the operations are equivalent. The complement is the smallest positive congruent remainder of a negative number, so adding a negative number and subtracting a positive number can both be represented by adding a complement.
  • How to calculate the complement: The complement of a positive number is itself; for a negative number, find the smallest positive congruence (modulo the capacity of the value bit) and put it into the value bit, with the sign position being 1 , get the complement of a negative number.

If you want to read more related articles, please visit PHP Chinese website! !

The above is the detailed content of Why are negative numbers in computers stored in two's complement?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
把逻辑地址转换为内存物理地址的过程称作什么把逻辑地址转换为内存物理地址的过程称作什么Jul 14, 2022 pm 02:52 PM

把逻辑地址转换为内存物理地址的过程称作“重定位”。程序发出的逻辑地址并不是数据或指令的真实存放地,但可以对这个地址进行转换来获得真实存放地的物理地址,相当于重新定位一次。

根据计算机网络覆盖范围,可将计算机网络分为哪几类根据计算机网络覆盖范围,可将计算机网络分为哪几类Jul 12, 2022 pm 05:13 PM

根据计算机网络覆盖范围,可将计算机网络分为三类:1、局域网(LAN),是一种在小区域内使用的,由多台计算机组成的网络,覆盖范围通常局限在10千米范围之内;2、广域网(WAN),是一种远程网,涉及长距离的通信,覆盖范围可以是个国家或多个国家,甚至整个世界;3、城域网(MAN),其网络覆盖范围通常可以延伸到整个城市,借助通信光纤将多个局域网联通公用城市网络形成大型网络。

微机的字长是4个字节这意味着什么微机的字长是4个字节这意味着什么Jul 08, 2022 pm 12:04 PM

微机的字长是4个字节意味着:在CPU中整体传输和处理的二进制数为32位。因为一个字节是8位长(字长),所以4个字节就是32位了,也就是说cpu中能够同时处理32位的二进制数据。在计算机领域,字是用来表示一次性处理事务的固定长度;一个字的位数,即字长,是计算机一次可处理的二进制数字的数目。

微型计算机的性能主要取决于什么微型计算机的性能主要取决于什么Jul 13, 2022 pm 03:40 PM

微型计算机的性能主要取决于CPU(中央处理器)的性能。CPU是计算机系统的运算和控制核心,是对计算机的所有硬件资源(如存储器、输入输出单元) 进行控制调配、执行通用运算的核心硬件单元;中央处理器(CPU)的性能对计算机性能起决定性作用。

微型计算机的运算器控制器及内存储器的总称是什么微型计算机的运算器控制器及内存储器的总称是什么Jul 14, 2022 pm 02:39 PM

微型计算机的运算器控制器及内存储器的总称是“主机”。在微型计算机中,运算器、控制器、存储器三个部分是信息加工、处理的主要部件;运算器和控制器总称为CPU(中央处理单元),而CPU与内存储器又总称为主机,这是计算机系统中最核心的硬件部分。

在计算机网络中使用MODEM时,它的功能是什么在计算机网络中使用MODEM时,它的功能是什么Jul 11, 2022 pm 12:02 PM

MODEM的功能为“实现模拟信号与数字信号之间的相互转换”。MODEM的中文名为“调制解调器”,它可以在发送端通过调制将数字信号转换成通信线路上传输的模拟信号,在接收端通过解调再将模拟信号转换为数字信号。

世界上第一台计算机的电子元器件是啥世界上第一台计算机的电子元器件是啥Jul 05, 2022 am 10:37 AM

世界上第一台计算机的电子元器件是“电子真空管”。世界上第一台计算机是“阿塔纳索夫-贝瑞计算机”,通常简称ABC计算机,采用电子真空管作为电子元件;该计算机电路系统中装有300个电子真空管执行数字计算与逻辑运算,机器使用电容器来进行数值存储,数据输入采用打孔读卡方法,还采用了二进位制。

计算机系统的内部总线主要可以分为哪些计算机系统的内部总线主要可以分为哪些Jul 11, 2022 pm 02:38 PM

计算机系统的内部总线主要可以分为5类:1、数据总线,在CPU与RAM之间来回传送需要处理或是需要储存的数据;2、地址总线,用来指定在RAM之中储存的数据的地址;3、控制总线,将微处理器控制单元的信号,传送到周边设备;4、扩展总线,是外部设备和计算机主机进行数据通信的总线,例如ISA总线,PCI总线;5、局部总线,取代更高速数据传输的扩展总线。

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)