C language--source code, inverse code, complement

Source: Internet
Author: User

We all know that 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 digits, so 1 bytes can represent 256 different kinds of information, that is, 28 (0 and 12 possible, 8-bit permutations), according to the arrangement. For example, an unsigned integer (unsigned char) that defines a byte size, it can represent the number of 0~255 (0~28-1), a total of 256 digits, because, as mentioned earlier, a byte can only represent 256 different kinds of information. Don't stop, or a byte unsigned integer, we're going to dissect it further, 0 is the smallest of these numbers, let's assume that it's inside the computer with a 8-bit binary representation of 00000000 (in theory it can also be expressed as a different binary code, As long as these 256 numbers each corresponding to the binary code is not the same. Assuming that 1 is 00000001, 2 is 00000010, 3 is 00000011, and so on, the largest number 255 is represented as the maximum number 8 in the 11111111-bit binary. We then convert these binary codes into decimal to see exactly the same number as we assumed, in fact, in a computer, unsigned integers are stored by this principle, so tell you the binary code of an unsigned integer, you can know how much this number is, and know that in the computer, This 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 and convert it, which represents a value of 2, and, unlike before, it takes up 2 bytes of memory. Different types account for different memory space, if the char is 1 bytes in My computer, the int is 4 bytes, and Long is 8 bytes (depending on your computer settings), the difference is simply that the memory is large enough to represent different information, That is to say the range of the number of larger (unsigned int can be expressed in the range is 0~28*4-1), as for how to calculate, in fact, is the same, directly to the binary and decimal conversion, binary is it in the computer appearance, The decimal is the number we represent (misunderstanding: the principle of different computer storage is different, depending on the merchant's

Preferences). unsigned integers simply do not have the original code, the inverse code, and the complement.
Only signed integers have the original code, the inverse code and the complement. None of the other types. Although we can also use the smallest number of binary to correspond to the smallest negative numbers, the largest also corresponds to, but that is not scientific, the following to say the scientific method. Or a byte of an integer, but this time it is signed, 1 bytes It is no matter how or only 256 number, because there are symbols so we have to express it as a range:-128-127. How is it stored in the computer? It can be understood that the highest level of sign bit, if it is 0 for positive numbers, if 1 for negative numbers, the remaining 7 to store the absolute value of the number, can represent the absolute value of 27 numbers, and then consider 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, which in turn means 1 to 127. Then these numbers correspond to the binary code is the original code of these numbers. A lot of people here will wonder if that negative number is from 10000001 to 11111111--1 to 127--then you find that it's not, and then there's only 255, because 10000000 doesn't take it into account. In fact, 10000000 represents the smallest negative integer in the computer, which is here-128, and actually does not represent from 10000001 to 11111111 in turn-1 to 127-but just the opposite, From 10000001 to 11111111, in turn, from 127 to 1. The negative integer is stored in the computer in the complement form, how does the complement be expressed, here also introduce another concept---------code, the so-called inverse code is the original code of negative numbers (negative source code and its absolute value of the corresponding original code is the same as the absolute value of the same number of original code is the same) each bit by bit to reverse, is 1 to replace 0, is 0 to replace 1, such as-1 of the original code is 00000001, and 1 of the original code is the same, then-1 of the inverse code is 11111110, and the complement is on the basis of the inverse code plus 1, that is, 1 of the complement is 11111110+1= 11111111, so we can figure out that 1 is stored on the computer by 11111111. To sum up, the computer stores signed integers, is used to store the complement of the integer, 0 of the original code, the complement is 0, a positive source code, the complement can be specifically interpreted as the same, minus the complement is its inverse code plus 1. Let me give you 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 inverse code complement
47 00101111 00101111 00101111 (positive complement and original code, reverse code, no literal understanding)
-47 10101111 11010000 11010001 (negative complement is added 1 on the counter code, sign bit does not participate in the operation)
For example, students of C language should have done this problem:
The 1 is output in unsigned type, and what is the result. (The procedure is as follows)

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

First of all, in My computer, the short int type of storage space is 2 bytes, you may be different, I said, depending on your computer configuration. It can store 28*2=65536 a 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, and we assign it to-1, which, according to what we said earlier, is stored in a computer with a complement of 11111111 11111111, noting that it is 2 bytes in front of it. If you force it to be an unsigned short integer output, so let's just think of the binary as an unsigned integer stored in the computer form, treatment of unsigned integral type there is no original code, inverse code and the concept of complement, direct 11111111 11111111 into the decimal is 65535, In fact, we look at the first to know that it is the largest number of the range. Oh, it's so simple. You have the source code compiled to run a look, if your computer short int is also two bytes, it will be the same result as mine. You can use this statement first to see how much storage space the:cout<<<endl;<> in your computer is, or to use sizeof to see the storage space allocated by any other type.
The last thing I'd like to remind you about is how data is stored in your computer, it's only for integer data, and it's another way for floating-point types, and we're not going to go into this for a while.
FeedBack:

1. Why use the complement form:

In fact, the numerical value of the computer with the complement to express, one is to prevent 0 have 2 coding, followed by the subtraction of the operation of the addition to express, to achieve the function of simplifying the circuit. Please refer to some professional books, such as the "Logic Design" published by Central China Cortine (hehe, my sophomore textbook).
Why is a signed integer represented by a complement. For example, a 8-bit integer indicates a range of -128~127<

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.