Original code, reverse code, and supplementary code in the computer

Source: Internet
Author: User

In a computer system, integers are all expressed (stored) by supplementary codes ).

In a computer, integers are represented by supplementary codes. the symbol bit and other bits can be processed in a unified manner, and the subtraction can also be processed by addition. In addition, when two bits are added, if the highest bit (symbol bit) has an inner, the carry is discarded. Therefore, you only need to deploy the addition circuit and the complement circuit in the computer to complete the addition of All integers.

 

The unsigned integer does not have the original code, reverse code, or complement code. Only signed integers have the original code, reverse code, and complement code! None of the other types.

Although we can also use the smallest number in the binary to correspond to the smallest negative number, the largest is also relative, but that is not scientific. The following describes the scientific method. It is a byte integer, but this time it is signed. one byte can only represent the number of 256. Because it is signed, it is expressed as a range: -128-127. How is it stored in a computer? It can be understood that the highest bit indicates the symbol bit. If it is 0, it indicates a positive number. If it is 1, it indicates a negative number. If the remaining seven digits are used to store the absolute values of the number, they can represent the absolute values of 27, in either case, the number is 27*2 or 256. First, we define that 0 is stored as 00000000 in the computer. For positive numbers, we can still convert them like the unsigned number. values from 00000001 to 01111111 indicate 1 to 127 in sequence. The binary codes corresponding to these numbers are the original codes of these numbers. Many people will think about whether the negative numbers represent-1 to-10000001 in sequence from 11111111 to 127. If so, there will be only 255 numbers in total, because 10000000 of the cases are not taken into account. In fact, 10000000 represents the smallest negative integer in the computer, that is,-128. In fact, it does not represent-1 to-10000001 in sequence from 11111111 to 127, but is the opposite, from 10000001 to 11111111 indicates-127 to-1. Negative integers are stored in the form of supplementary codes in the computer. How is the expression of the Supplementary Code? Another concept-anticode, the so-called anticode is to reverse each bit of the negative original code (the negative original code is the same as the original code corresponding to its absolute value, simply put, it is the same as the absolute value of the original code, if it is 1, it is changed to 0. If it is 0, it is changed to 1. For example, if the original code of-1 is 00000001 and the original code of 1 is the same, the anticode of-1 is 11111110, the Supplementary Code is to add 1 on the basis of the anticode, that is, the Supplementary Code of-1 is 11111110 + 1 = 11111111, so we can calculate that-1 is stored in the computer by 11111111. To sum up, when a computer stores signed integers, it uses the complement code of this integer for storage. The source code and the complement code of 0 are both 0, and the source code and the complement code of positive numbers can be specially understood as the same, the complement of a negative number is its anticode plus 1.

Here are a few more examples to help you understand!

Decimal → binary (how to calculate? If you do not know how to read basic computer books)
47 → 101111

Signed integer original code anti-Code complement
47 00101111 11010000 00101111 (same as the original code)
-47 00101111 11010000 11010001 (the negative complement is to add 1 to the anti-code)

For another example, students who want to learn C language should have done this:
What is the result of outputting-1 as an unsigned type? (The procedure is as follows)

# Include <iostream. h>
Void main ()
{
Short int n =-1;
Cout <(unsigned short INT) n <Endl;
}

First, in my computer, the short int type storage space is 2 bytes. You may be different, as I said, it depends on your computer configuration. It can store 28*2 = 65536 different data records. If it is unsigned, its range is 0 ~ 65535 (0 ~ 216-1). If it is signed, its range is-32768 ~ 32767 (-215 ~ 215-1 ). In this question, n is a signed short integer variable, and we assign it-1. According to what we mentioned above, it is stored in a computer with a complement code of 11111111 11111111, note that it is two bytes. If it is forced to be output as a non-Signed short integer, then we will regard the binary as the form of the unsigned integer stored in the computer, there is no concept of the original code, anti-code, or complement code for unsigned integer types. The conversion of 11111111 11111111 to decimal is 65535, as a matter of fact, we can see that it is the largest number in the range. It's that simple. You can compile and run the above source code. If your computer's short int is also two bytes, it will have the same result as mine. You can use this statement to check the storage space occupied by the cout <sizeof (short INT) <Endl, you can also use sizeof to view the storage space allocated by any other type.

How to store data in a computer is only applicable to integer data. For floating-point data, we will not go into detail here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.