The original code, the inverse code and the complement in the computer

Source: Internet
Author: User

See this title, a lot of people have something to say, cut! This thing every computer basic knowledge of the book is introduced, you also take out show what! My principle is that you need to have a look, understand and do not bother, and do not have to be sarcastic, I believe there will always be people who need it. At the beginning, I did not read a book to understand, in the Internet to find a lot of information to be enlightened.

A few days ago with his wife to talk about the original code, anti-code and the complement of knowledge, wife indefinitely, here I published my personal opinion, simple to my understanding of the original code, anti-code and complementary knowledge summed up, one can give the people do not understand the revelation, and second can also be convenient for the wife after the memory review. Understand that there is a wrong place, I hope you will point out, thank you!

  Everyone knows that the data is stored in bytes in the computer, 1 bytes equals 8 bits (1byte=8bit), and the computer can only recognize 0 and 1 of these two numbers, so according to the arrangement, 1 bytes can represent 256 different information, namely 28 (0 and 12 possible, 8-bit arrangement), For example, if you define a byte-sized unsigned integer (unsigned char), then it can represent 0~255 (0~28-1), which is a total of 256 numbers, because, as stated earlier, a byte can represent only 256 different information. Do not stop, or a byte of unsigned integer, let us further dissect it, 0 is the smallest of these numbers, we first assume that it in the computer with 8-bit binary representation as 00000000 (in theory can also be expressed as a different binary code, As long as the 256 numbers correspond to the binary code of each number is different, then assume that 1 is represented as 00000001, 2 is 00000010, 3 is 00000011, and so on, then the largest number 255 in the 8-bit binary is represented as the largest number 11111111, Then, we convert these binary codes into decimal to see exactly the same as the number we assumed, and in fact, in the computer, unsigned integers are stored in this principle, so tell you the binary code of an unsigned integer, and you'll know what the number is, and know that in the computer, The number itself is stored in this binary code. For example, I give you a 2 byte size binary code, first declare that it represents an unsigned integer: 00000000 00000010, we omit the preceding 0, the conversion, it represents the value of 2, and the previous difference is that it accounted for 2 bytes of memory. Different types account for different memory space, such as in My Computer char is 1 bytes, int is 4 bytes, Long is 8 bytes (you may be different, depending on different computer settings), they are only different memory large can represent different information more, That is to say that the number of larger range (unsigned int can be expressed in the range is 0~28*4-1), as to how to calculate, in fact, are the same, directly binary and decimal conversion, binary is its appearance in the computer, the decimal is the number we represent. Ah ha, originally these are can calculate, I once thought that the different computer storage principle is different, depends on the merchant's liking, hehe. Said so much how has not mentioned the original code, anti-code and complement Ah, do not hurry, impatient to eat hot tofu, hehe, because unsigned integers there is no original code, anti-code and complement. (Ah, it is not deceived, 5555 "" I told my mother to go, my brother bullied me) all said don't worry, you don't want to think I said so half a day unsigned integer, then the signed integer how to doHuh?

Oh, yes, only signed integers have the original code, anti-code and complement! None of the other types. Although we can also use the smallest number in the binary to correspond to the smallest negative numbers, the largest also corresponds, but that is unscientific, the following is a scientific approach. Or a byte integer, but this time it's signed, 1 bytes It can only represent 256 numbers anyway, because there is a sign so we represent it as a range:-128-127. How is it stored in the computer? It can be understood that the symbol bit with the highest level, if 0 is a positive number, if it is 1 for negative numbers, the remaining 7 bits to store the absolute value of the numbers, can represent the absolute value of 27 numbers, and then consider the positive and negative two cases, 27*2 or 256 number. First, the definition of 0 is stored in the computer as 00000000, and for positive numbers we can still convert like unsigned numbers, from 00000001 to 01111111 in turn 1 to 127. Then these numbers correspond to the binary code is the original code of these numbers. Many people will think that the negative number from 10000001 to 11111111 in turn represents-1 to-127, then you find no, if so then there are only 255 numbers, because 10000000 of the situation is not taken into account. In fact, 10000000 represents the smallest negative integer in the computer, which is the-128, and it is not actually from 10000001 to 11111111, in turn, 1 to-127, but just the opposite, From 10000001 to 11111111 in turn-127 to-1. Negative integers in the computer is in the form of a complement of storage, complement is how to express, here also introduce another concept-anti-code, the so-called anti-code is the negative number of the original code in addition to the sign bit (the original code of negative numbers in addition to the symbol bit and its absolute value corresponding to the same source code), Simply said that the absolute number of identical numbers of the same size, the individual bits are reversed, is 1 to 0, is 0 to 1, such as 1 of the original code is 0000001 (note that there is only 7 bits, do not look at the sign bit, I say the negative sign bit is 1), and 1 of the original code is the same, Then-1 of the anti-code is 1111110 (this is also 7 bits, followed by the sign bit is 8-bit), and the complement is on the basis of the anti-code plus 1, that is-1 of the complement is 11111110+1=11111111, so we can figure out-1 in the computer is stored by 11111111.summing up, the computer stores a signed integer, is used in the complement of the integer storage, 0 of the original code, the complement is 0, a positive number of the original code, the complement can be a special understanding of the same, negative complement is its anti-code plus 1. Here are a few more examples to help you understand!



decimal → binary (how to calculate?) If you don't know how to read a computer-based book)
47→101111

Signed integer original code anti-code complement
47 00101111 00101111 00101111 (positive complement and original code, inverse code is the same, can not literally understand)
-47 10101111 11010000 11010001 (negative complement is added 1 on the inverse code)

For example, students in C language should have done this problem:
What is the result of exporting 1 as an unsigned type? (procedure is as follows)

#include
void Main ()
{
short int n=-1;
cout<< (unsigned short int) n<<endl;
}

First in My Computer the short int type of storage space is 2 bytes, you may be different, as I said, depending on your computer configuration. It can store 28*2=65536 different data information, if it is unsigned then its range is 0~65535 (0~216-1), if it is signed, then its range is -32768~32767 ( -215~215-1). In this topic, start N is a signed short integer variable, we assign it to-1, according to what we said earlier, it is in the computer in the complement of 11111111 11111111 stored, note that the previous said is 2 bytes. If it is forced to an unsigned short integer output, then we will just consider the binary as an unsigned integer in the form of a computer storage, the treatment of unsigned integral type there is no original code, anti-code and the concept of the complement, the direct conversion of 11111111 11111111 to decimal is 65535, As a matter of fact, we all know that it is the largest number in the range. Oh, it's so simple. You have to compile the above source code to see if your computer short int is also two bytes, it will be the same as I have the result. You can first use this statement to see:cout<<sizeof (short int) <<endl;<= "" font= "> Look at the amount of storage space in your computer, or you can use sizeof to see the storage space allocated by any other type.

Finally, I would like to remind you, about how the data is stored in the computer, this only applies to integer data, for floating-point type is another way, here we do not delve into the moment.

The original code, the inverse code and the complement in the computer

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.