1, you have decided whether you need to have positive or negative:
Just as we have to decide whether a quantity uses integers or real numbers, and how many ranges we use, we have to decide for ourselves if a certain quantity needs positive or negative. If this quantity does not have a negative value, then we can set it as a positive or negative type.
In a computer, you can distinguish between positive and negative types, called character types (signed), and positive types (only positive values), which are called no type. (unsigned) numeric types are integral or solid, where the integral type is divided into no type or character type, and the solid type is only a character type. Character types are also divided into characters and no type. For example, there are two quantities, age and inventory, we can set the former as a character type without character, which is the integer type with character.
2. Use the highest digit in the two number to indicate positive or negative:
First you have to know which one is the highest? 1-byte types, such as character types, the highest digit is the 7th digit, the 2-byte number, the highest digit is the 15th digit, the 4-byte number, and the highest digit is the 31st digit. numeric types of different lengths have the highest digits, but always the leftmost one (as indicated below). The character type is fixed to 1 bytes, so the highest bit is always the 7th digit.
11111111 11111111 11111111 11111111
When we specify that a quantity is an unsigned type, then its highest bit of 1 or 0, like other bits, is used to indicate the size of the number.
When we specify that a quantity is an unsigned type, at this point the highest number is called the sign bit. is 1 o'clock, indicating that the number is negative and positive when 0.
3. The range difference between unsigned numbers and signed numbers:
In unsigned numbers, all bits are used to directly represent the size of the value. The highest position in a signed number is used to indicate positive or negative, so the maximum value of the number becomes smaller when it is positive. We give a byte of numerical comparison:
Unsigned number: 11111111 value: 255
Number of symbols: 01111111 value: 127
Eg: (Ben) con immigrations from the D grammar to play the mathematical formula, the power of 2 square)
is also a byte, the maximum number of unsigned numbers is 255, and the maximum number of signed numbers is 127. The reason is that the highest digit in the signed number is moved to represent the symbol. And, we know that the highest weight is also the highest (for 1 bytes is 2 of the 7 square =128), so only less than one, the maximum value in half a sudden.
However, the advantage of a signed number is that it can represent negative numbers. So, although it shrinks at its maximum value, it stretches in a negative direction. We still have a byte of numerical contrast:
Unsigned number: 0 ————— –255
Number of symbols: -128 ——— 0 ———-127
Also a byte, the minimum unsigned value is 0, and the minimum number of signed numbers is-128.
So the number of different values that can be expressed by the two is equal to 256. But the former expresses 0 to 255 of these 256 digits, the latter expressing
Yes-128 to +127 of these 256 numbers.
How is the minimum value of a signed data type calculated?
The maximum value of a signed data type is calculated exactly as unsigned, except that it has a single highest bit (see 3rd). But in negative
Range, the numerical method cannot be converted directly using the 1* 26 + 1* 25 formula.
In the computer, the negative number is in addition to the highest bit is 1, also uses the complement form to express. Therefore, the complement needs to be restored before the value is computed. Here, first visually look at the form of complementary complement:
In 10:1 for positive 1, plus minus sign:-1 for negative values relative to 1.
Well, it's easy to think that in 2 (1 bytes): 0000 0001 means positive 1, then the High is 1:1000 0001 should represent-1.
However, in fact, the rules in the computer are somewhat opposite, please look at the following table:
Binary
(1 bytes) |
Decimal Value |
10000000 |
-128 |
10000001 |
-127 |
10000010 |
-126 |
10000011 |
-125 |
10000100 |
-124 |
...... |
...... |
11111110 |
-2 |
11111111 |
-1 |
First we see, from-1 to-128, the top of the binary is 1, as we said earlier, the highest number is 1, and then we find it a little odd, 1000 0000 is not used to represent 0, and 1000 0001 is not a visual representation-1, in fact,-1 with 1111 1111来 said.
How do you understand this problem? First question is-1 or-128 big?
Of course--1 big, then, 1111 1111-1 what is it? Exactly the same as the actual results. 1111 1111-1=1111 1110, and 1111 1110 is-2, so keep going, and when you see the 1 accident with the highest bit left to represent the symbol, the other lows are all 0 o'clock, the smallest negative value, and in one byte the smallest negative value is 1000 0000, which is 128.
We use the 1-digit example to look at the number of integers in different byte numbers, and how to express-1.
Number of bytes | Binary
value |
Decimal Value |
Single byte number |
11111111 |
-1 |
Double byte number |
11111111 11111111 |
-1 |
Four-byte count |
11111111 11111111 11111111 11111111 |
-1 |
Perhaps some people see here, have been mixed up, why? 1111 1111 Sometimes means 255, sometimes it's 1. So I'm going to stress the 2nd, you decide whether a number is signed or unsigned, when writing a program, specify that a quantity is signed, then when the number of the binary of this quantity is 1 o'clock, it represents 1; Conversely, if you declare that the amount is unsigned in advance, At this point it represents the maximum allowable value for this amount, and for a byte number, the maximum value is 255.
We already know that all data in a computer is ultimately expressed using binary numbers. has also learned how to convert a 10-binary number to binary. However, we still do not learn how a negative number is expressed in binary. For example, suppose there is a number of int type, the value is 5, then we know that it is represented on the computer as:
5 converted to two is 101, but the number of int type occupies 4 bytes (32 bits), so a pile of 0 is filled in front. Now want to know,-5 how to express in the computer? In a computer, a negative number is expressed in the complement form of its positive value. What do you mean, "complement"? This has to start with the original code, the inverse code.
Original code: An integer, the number of binary numbers converted to the absolute size, the highest is the symbol bit, called the original code. Red for symbol bit
00000000 00000000 00000000 00000101 is 5 of the original code. 10000000 00000000 00000000 00000101 is-5 of the original code
Anti-code: the binary in addition to the number of symbols in the bitwise counter, the resulting new binary number is called the original binary number of the inverse code. The inverse code of the positive number is the original code, and the inverse of the negative number is the reverse of the original code sign.
The reverse operation refers to: the original 1, 0; originally 0, 1. (1 change 0; 0 Change 1)
positive number: The inverse code of a positive number is the same as the original code. Negative Number: Negative inverse code, symbol bit is "1", the numeric value part is reversed by bit.
For example: The 10000000 00000000 00000000 00000101 except for the symbol bit each counter,
11111111 11111111 11111111 11111010.
At this point, we say: 11111111 11111111 11111111 11111010 is a 10000000 00000000 00000000 00000101 Inverse code.
The inverse code is mutual, so it can also be called:
11111111 11111111 11111111 11111010 and 10000000 00000000 00000000 00000101 are reciprocal codes.
Complement: The inverse code plus 1 is called the complement. (if the last one is 1, go ahead and add 1)
1. Positive number: The complement of a positive number is the same as the original code.
2. Negative numbers: According to the rules
In other words, to get a number of the complement, first get the inverse code, and then the inverse code plus 1, the resulting number is called the complement.
11111111 11111111 11111111 11111010 is the inverse code of 10000000 00000000 00000000 00000101 (-5).
Add 1 to 11111111 11111111 11111111 11111011
So,-5 is expressed in the computer as: 11111111 11111111 11111111 11111011. Convert to 16
To take another example, let's look at how the integer 1 is represented in the computer.
Suppose this is also an int type, then:
1, first take-1 of the original code: 10000000 00000000 00000000 00000001
2, in addition to the symbol bit back to the back code: 11111111 11111111 11111111 11111110
3, add 1 to complement: 11111111 11111111 11111111 11111111 Visible,-1 in the computer with binary expression is all 1. 16 System: 0xFFFFFF.
The advantages of a computer with a number of symbols in the complement:
1. The conversion between the complement of a negative number and the complement of the corresponding positive number can be done with the same method--the complement operation can simplify the hardware;
2, can be reduced into addition, eliminating the subtraction device;
3, unsigned number and the addition of signed numbers can be completed with the same circuit.
Can come up with a method of calculating mental arithmetic--from the lowest start to find the first 1 are unchanged, the symbol bit unchanged, this between the "reverse" (this method is used only for questions)
Method |
Example 1 |
2 |
1, starting from the right, find the first "1" |
10101001 |
10101100 |
2, starting from this "1" to the leftmost (excluding the sign bit, the highest bit) |