Negative Number: The original code is the original representation method. The reverse code is used to retrieve the reverse Code except the sign bit (highest bit ). Complement = reverse code + 1 When I used to learn binary encoding, the teacher talked about a pile of original codes, reverse codes, supplementary codes, xxxx conversions, and negative expression methods, finally, I found a better way to explain it on the Internet. I saved it and shared it. However, I found some basic coding knowledge to systematically explain it. If I only want to see the negative number encoding memory method, please jump 1. If you do not know how to encode binary data, continue; otherwise, skip to 2. 1 byte = 8 bits, so the maximum number it can represent is of course 8 bits are all 1 (since the number of binary can only be 0 or 1, if it is our common 10-digit system, the 8-digit system will all be 9. In this case, you should understand it ?) The maximum number of 1-byte binary: 11111111. What is the size of this number? Let's convert it to a decimal number. No matter what the hexadecimal format is, the left is the high level, and the right is the low level. We are very accustomed to using the 10-digit counting method. The first one represents a few 1 (that is, a few 100), and the second one represents a few 10 (that is, a few 101 ), the third digit represents several hundred thousand (that is, several hundred thousand )..., In the elementary school textbooks, the number in a single place represents 1, the number in 10 represents 10, and the number in a hundred represents 100 ...... Likewise, the binary number is: the number of digits 1st represents a few 1 (20), the number of digits 2nd represents a few 2 (21), and the number of digits 3rd represents a few 4 (22 ), 4th bits indicate 8 (23 )...... We used to know that one byte has eight bits. Now, through computation, we know that the maximum number of 1 byte expressions is 255, that is, 0 ~ 255: the number of 256. So what about two bytes (two bytes? 16 bytes in total. 1111111111111111, this number is not big, but it looks a little dizzy. From now on, we should learn to express the binary number as follows: 1111 1111 1111 1111, that is, every four spaces. The maximum number of double bytes is: 1*215 + 1*214 + 1*213 + 1*212 + 1*211 + 1*210 + ...... + 1*22 + 1*21 + 1*20 = 65535 Naturally, we can think that the maximum allowed value of a data type is related to its number of digits. The specific calculation method is: if it has N bits, the maximum value is: Maximum N-bit binary: 1*2 (n-1) + 1*2 (n-2) +... + 1*20 2. Understand the number of signed and unsigned characters How do negative numbers be expressed in the computer? You may have heard two different answers. One is a textbook, which tells you that a computer uses a "complement" to indicate a negative number. However, the concept of "complement code" is a lesson. We need to use a chapter in Chapter 2 to talk about everything in binary notation. Furthermore, a negative number is expressed by a "complement". The formula is used to tell you how to calculate the answer to the question. But I didn't tell you why I could use this formula to answer the question? ----- I am confused by this >_< The other isProgramThe highest bit of the binary number represents the symbol. The highest bit is 0, indicating a positive number, and the highest bit is 1, indicating a negative number. This statement is true, but if it is not described below, it is wrong. At least it cannot explain why-1 of the character type is expressed as "1111 1111" in binary format (with a hexadecimal value of ff), rather than "1000 0001" that we can better understand ". (Why is the latter better understood? Since when the highest bit is 1, it indicates a negative number, isn't 1000 0001 exactly-1? ----- Re! This is what I thought at the beginning. So has been fighting in the mind, and the more confused it is. =, = ). Let's start from scratch. 2.1. You decide whether or not there must be positive or negative. Just as we must determine whether an integer or a real number is used to determine the number of ranges, we must determine whether a certain number must be positive or negative. If this quantity does not have a negative value, we can set it to a positive or negative type. In a computer, positive and negative types can be distinguished, which are called the operator type and the positive and negative types (only positive values. The numeric type can be an integer or a real type. The integer type can be a non-letter type or a character type, while the real type can only be a character type. Character types can also be divided into break type and break type. For example, if there are two quantities, age and stock, we can set the former as the character type without a character, and the latter as the integer type with a character. 2. Use the highest digit in the binary number to indicate positive and negative. First, you must know which one is the highest? 1-byte type, such as character type. The maximum value is 7th bits, the maximum value is 15th bits, and the maximum value is 31st bits. The highest bit of a value of different lengths is different, but it is always the leftmost (as shown below ). The character type is fixed to 1 byte, so the maximum bit is always 7th characters. (Red indicates the highest digit) Number of single words: 1111 1111 Double Bytes: 1111 1111 1111 1111 Four bytes: 1111 1111 1111 1111 1111 1111 1111 1111 When we specify a quantity that isUnsigned typeThe value of 1 or 0 is the same as that of other bits. When we specify a quantity that isSigned typeAt this time, the maximum number is called "symbol bit ". If the value is 1, the number is negative. If the value is 0, the value is positive. 3. the unsigned number and the number range are different. In the unsigned number, all bits are used to directly represent the size of the value. The highest bit in a signed number is used to indicate positive and negative values. Therefore, when it is a positive value, the maximum value of this number decreases. Let's take a byte value comparison: Unsigned quantity: 1111 1111 value: 255 1*27 + 1*26 + 1*25 + 1*24 + 1*23 + 1*22 + 1*21 + 1*20 Symbol Number: 0111 1111 value: 127 1*26 + 1*25 + 1*24 + 1*23 + 1*22 + 1*21 + 1*20 It is also a byte. the maximum value of the unsigned number is 255, and the maximum value of the signed number is 127. The reason is that the highest bit in the number of symbols is removed to represent the symbol. In addition, we know that the highest bit of the weight is also the highest (for the number of 1 byte is 2 to the power of 7 = 128), so only less than one bit, the maximum value is halved at once. However, the advantage of a signed number is that it can represent a negative number. Therefore, although it has shrunk in the maximum value, it is stretched in the negative direction. We still compare the values of one byte: Unsigned number: 0 --------------- 255 Signed quantity:-128 --------- 0 ---------- 127 It is also a byte. the minimum value of the unsigned value is 0, and the minimum value of the number of signed values is-128. Therefore, the numbers of different values can be the same as 256. However, the former expresses the numbers 0 to 255, and the latter expresses the numbers-256 to + 128. How is the minimum value of a signed data type calculated? The calculation method for the maximum value of the signed data type is exactly the same as that of the unsigned data type, except that the maximum bit is missing (see 3rd points ). However, within the negative value range, the calculation method of the value cannot be directly converted using the Formula 1*26 + 1*25. In a computer, a negative number is expressed in the form of a supplementary code in addition to the highest bit of 1. Therefore, you need to restore the complement code before calculating its value. Here, you can take a look at the form of complement code: In our original mathematical experience, in the 10th hexadecimal notation: 1 represents positive 1, and with a negative number:-1 represents a negative value relative to 1. Then, we can easily think that in binary (1 byte): 0000 0001 indicates positive 1, then after the High is 1: 1000 0001 should indicate-1. However, in fact, the computer rules are somewhat different. See the following table:
binary value (1 byte) |
decimal value |
1 000 0000 the Red 1 represents a negative number, and the blue is a complementary code (complement = reverse code + 1) |
-128 |
1 000 0001 what is the value of the blue part?: Restore the complement code to the original code |
-127 convert to negative number?: minus 1 and then bitwise inversion |
1 000 0010 restoration method: complement-1 and reverse retrieval |
-126 |
1 000 0011 |
-125 |
... |
... |
1111 1110 |
-2 |
1 111 1111 |
-1 |
First, we can see that from-1 to-128, the highest binary bits are 1 (the table is marked in red), as we learned earlier. Then we found that 1000 0000 was not used to represent-0, and 1000 0001 was not used to visually represent-1. In fact,-1 is represented by 1111 1111. How can this problem be solved?First, you have to ask whether the value is-1 or-128.? Of course it is-1. -1 is the largest negative integer. In the computer, whether it is a character type or an integer type, or whether the integer is several bytes. It uses all 1 to represent-1. For example, if 1111 1111 represents-1 in a byte value, what is 1111 1111-1? It is exactly the same as the calculation result in reality. 1111 1111-1 = 1111 1110, and 1111 1110 is-2. In this way, the minimum negative value is 1000 in one byte, when only the highest bit is used to indicate the 1 of the symbol, and all the other low values are 0, that is,-128. -------- Annotation: This is the blue text, so that I can finally remember the encoding method of-1, Khan =. = Let's take-1 as an example to see how to express-1 in Integers of different bytes:
Bytes |
Binary Value |
Decimal Value |
Number of Single-word segments |
1111 1111 red indicates that the complement of the negative blue part is 1 |
-1 |
Negative number: the original code is the original representation method. The reverse code is the reverse Code except the symbol bit (highest bit) and the complement code = the reverse code + 1 double byte. |
1111 1111 1111 1111 |
-1 |
Four bytes |
1111 1111 1111 1111 1111 1111 1111 |
-1 |
Some people may be confused at this time: Why does 1111 1111 sometimes indicate 255 and sometimes-1? So I want to emphasize the 2nd point mentioned above: You decide whether a number is signed or unsigned. When writing a program, specify a volume that is signed. When the binary value of this volume is 1, it indicates-1. On the contrary, if you declare the value as unsigned, it indicates the maximum value allowed by the value. For the number of bytes, the maximum value is 255. Okay, I have excerpted the interim section. In fact, some basic data type knowledge about C is very detailed in the original article, which is too long. After I have picked the content I need, I didn't have a full post, if you need to learn, we recommend that you refer to the original article :) From http://blog.cersp.com/7892477/1201309.aspx Keywords: binary encoding, negative binary, binary |