Why 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.
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, -16
These 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The 2024CSRankings National Computer Science Major Rankings have just been released! This year, in the ranking of the best CS universities in the United States, Carnegie Mellon University (CMU) ranks among the best in the country and in the field of CS, while the University of Illinois at Urbana-Champaign (UIUC) has been ranked second for six consecutive years. Georgia Tech ranked third. Then, Stanford University, University of California at San Diego, University of Michigan, and University of Washington tied for fourth place in the world. It is worth noting that MIT's ranking fell and fell out of the top five. CSRankings is a global university ranking project in the field of computer science initiated by Professor Emery Berger of the School of Computer and Information Sciences at the University of Massachusetts Amherst. The ranking is based on objective

Windows Remote Desktop Service allows users to access computers remotely, which is very convenient for people who need to work remotely. However, problems can be encountered when users cannot connect to the remote computer or when Remote Desktop cannot authenticate the computer's identity. This may be caused by network connection issues or certificate verification failure. In this case, the user may need to check the network connection, ensure that the remote computer is online, and try to reconnect. Also, ensuring that the remote computer's authentication options are configured correctly is key to resolving the issue. Such problems with Windows Remote Desktop Services can usually be resolved by carefully checking and adjusting settings. Remote Desktop cannot verify the identity of the remote computer due to a time or date difference. Please make sure your calculations

<p>MSTeams is the trusted platform to communicate, chat or call with teammates and colleagues. Error code 80090016 on MSTeams and the message <strong>Your computer's Trusted Platform Module has failed</strong> may cause difficulty logging in. The app will not allow you to log in until the error code is resolved. If you encounter such messages while opening MS Teams or any other Microsoft application, then this article can guide you to resolve the issue. </p><h2&

The "e" of computer is the scientific notation symbol. The letter "e" is used as the exponent separator in scientific notation, which means "multiplied to the power of 10". In scientific notation, a number is usually written as M × 10^E, where M is a number between 1 and 10 and E represents the exponent.

The meaning of cu in a computer depends on the context: 1. Control Unit, in the central processor of a computer, CU is the component responsible for coordinating and controlling the entire computing process; 2. Compute Unit, in a graphics processor or other accelerated processor, CU is the basic unit for processing parallel computing tasks.

Occasionally, the operating system may malfunction when using a computer. The problem I encountered today was that when accessing gpedit.msc, the system prompted that the Group Policy object could not be opened because the correct permissions may be lacking. The Group Policy object on this computer could not be opened. Solution: 1. When accessing gpedit.msc, the system prompts that the Group Policy object on this computer cannot be opened because of lack of permissions. Details: The system cannot locate the path specified. 2. After the user clicks the close button, the following error window pops up. 3. Check the log records immediately and combine the recorded information to find that the problem lies in the C:\Windows\System32\GroupPolicy\Machine\registry.pol file

One's complement is a number representation commonly used for binary number arithmetic in computers. The complement code simplifies the addition and subtraction operations of negative numbers and can represent a wider range of integers. The use of the complement code plays an important role in computer science and is very important for understanding the operation and representation of integers in computers.

Solution to the problem that steam cannot connect to the remote computer: 1. In the game platform, click the "steam" option in the upper left corner; 2. Open the menu and select the "Settings" option; 3. Select the "Remote Play" option; 4. Check Activate the "Remote Play" function and click the "OK" button.