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

Source: Internet
Author: User
We all know that data is stored in bytes in the computer. one byte equals 8 bits (1 byte = 8 bits), while the computer can only recognize the numbers 0 and 1, therefore, according to the arrangement, energy saving for one word represents 256 different types of information, that is, 28 (0 and 1 are two possibilities, and 8 bits are arranged ), for example, to define an unsigned integer (unsigned char) of the byte size, it can represent 0 ~ 255 (0 ~ 28-1) these numbers are 256 in total, because, as mentioned earlier, one byte can only represent 256 different types of information. Don't stop. It's still a byte unsigned integer. Let's further analyze it. 0 is the smallest of these numbers, let's assume that it is represented as 00000000 in an 8-bit binary code inside the computer (theoretically, it can also be represented as another different binary code, as long as the 256 numbers correspond to different binary codes), then suppose 1 represents 00000011, 1 represents, and 3 represents, and so on, then the maximum number 255 is expressed as the maximum number 11111111 in the 8-bit binary. Then, we can convert these binary codes into decimal digits, we will find that the number is exactly the same as the number we assume. In fact, in the computer, the unsigned integer is stored according to this principle, so tell you the binary code of an unsigned integer, you can know the number, and in the computer, the number itself is stored in this binary code. For example, I will give you a two-byte binary code. First, declare that it represents an unsigned integer: 00000000 00000010. We will omit the preceding 0 and convert it to another one, it also indicates the value 2. Unlike the previous one, it occupies 2 bytes of memory. Different types occupy different Memory Spaces. For example, in my computer, char is 1 byte, Int Is 4 byte, and long is 8 byte (You may be different, it depends on different computer settings). The difference is that the memory size can indicate more different information, that is, the number range that can be expressed is greater (the unsigned int can represent 0 ~ 28*4-1). As for how to calculate it, it is actually the same. Convert the binary and decimal values directly. The binary value is what it looks like in the computer, and the decimal value is what we represent. Aha, it turns out that this is computable. I used to think that the storage principles of different computers are different depending on the preferences of sellers. Why haven't we mentioned the source code, anti-code, and re-code so much? Don't worry, you can't eat hot tofu, because unsigned integers do not have the original code, reverse code, or complement code. (Ah, it's not a scam. I told my mom to go and my brother bullied me.) I said don't worry, you don't have to think about the unsigned integer that I 've been talking about for so long. What should I do with a signed integer?

Yes. 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? (ProgramAs shown below)

# 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 put the aboveSource codeCompile and run it. If your computer 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.

the last note is that data stored in a computer is only applicable to integer data. For floating-point data, this is another method. We will not go into it for the time being.

Related Article

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.