Number system
It had been written for a long time, did not expect to restore the mentally retarded, got to the present. Here is a record of how I felt then. One morning it was so wasted. It took one morning and one night to sort out the ghosts.
As for numeric values, there are no basic concepts such as binary, decimal, and so forth, only a few major points of knowledge.
1, the conversion between the numbering
(1) Some concepts about binary
Here is the main record of bits, bits for the binary description. These things are also very troublesome to describe, perhaps I have not mastered it.
Bit width/bit: A binary number, has its bit width, how many 0/1, its bit width is how much, for example, binary number 10110, its bit width is 5, from No. 0 to 5th bit, also say this is a 5 bit wide binary number, this is a binary number size is 5 bit.
Highest and lowest bits: for the above 10110, the highest bit is 1, the lowest bit is 0, the highest bit is the 4th bit, the lowest bit is 0
(2) binary conversion to decimal :
① binary conversion to decimal method is: The binary number by the right to expand, add the decimal number.
② Example: binary number 10011.01, the number of digits 1 has 4th, 1th, No. 0, 2, then there are:
10011 Decimal value (Note that the default is the size of the decimal hours) for: 2^4 + 2^1 + 2^0 + 2^ (-2) = 19.25
decimal Conversion to binary:
① Conversion method is: The integer part, in addition to the second surplus; fractional part, multiply by two rounding (the fractional part will usually indicate how many digits to be accurate to the decimal point).
② Example: Convert 35.63 to binary number, fractional to 3 digits after decimal point
So for the integer part, except for the two remainder
The binary number of the integer part is 100011.
For fractional parts: multiply by two rounding
0.63*2 = 1.26, takes 1;0.26*2 = 0.52, takes 0;0.52*2 = 1.04, takes 1, has reached three bits. So the fractional part is 101.
So the binary representation of 35.63 is 100011.101.
(3) binary conversion into octal :
① method: From the decimal point to the two sides, each three-bit binary divided into a group, each group of decimal is the corresponding octal, (note that the highest bit or the lowest bit is not enough 3 bits to fill 0).
② Example: 1001.01 converted to octal, grouped (00) 1 001. 01 (0), converted to octal is 11.2.
Octal into binary:
① octal conversion to binary binary method with binary conversion to octal instead, an octal corresponds to a three-bit binary, which in turn can be expanded.
② Example: Octal 67.21 is converted to binary, 6 is 110,7 is 111,2 is 010,1 is 001, so the corresponding binary is 110111.010001.
(4) binary conversion to 16 binary, 16 binary into binary:
binary conversion to hexadecimal/16 binary to binary is similar to octal, except that octal corresponds to 3 bits, while hexadecimal corresponds to 4 bits; it is not detailed here.
The conversions between them can be done in binary.
2. binary binary Signed and unsigned
(1) unsigned number , that is, no positive or negative points, the default is positive, the above is said to be positive, that is, unsigned number. The same is true for binary.
• In general, there is no special support for this number is signed, the default is unsigned number.
The • n bit unsigned can represent the range bit 0~+ (2^n-1), which is 0~1...1
(2) signed number , as the name implies is positive or negative points, such as +39,-49. For binary numbers, there is also the number of symbols. In the future I recorded the number of symbols, the default refers to the number of symbols of the binary number.
① in a signed binary number, the highest bit represents the sign bit, the other bits represent the other contents of the number, where the sign bit is 1 o'clock, indicating that the signed binary number is negative, and the sign bit is 0 o'clock, indicating that the signed binary number is positive.
For example, signed number 10010, the highest bit is 1, indicating that the binary number is negative. (Pay attention to the following anti-code, of course)
• From the previous above you can know that a signed binary number, at least two bits, one of which is the sign bit.
② signed binary number has three kinds of expression: the original code, anti-code, and complement (the decimal and the like also have the complement of the same, here only records and binary related).
③ Source code : The highest bit is the sign bit, except the highest bit binary number, which represents the absolute value of the binary value.
For example, the original code 100110, the highest bit is 1, negative, the remaining is 00110, the value is 6, then the original code represents the size of the value is-6.
• In general, when it comes to a signed number, it is generally referred to as the original code.
• 0 of the original code, can be expressed as +0 and-0, that is, 1 ... 0,0..0, so there are two forms of expression.
The • N-bit source represents a range of:-(2^ (n-1)-1) ~+ (2^ (n-1)-1), or 1 ... 1~0...1
④ Anti- code: The anti-code can be derived from the original code, the original code of every bit to get the corresponding anti-code.
For example, a signed number of the original code is 10011, then this signed number of the inverse code is 01100.
• Anti-code in the application is not much, perhaps I have not learned it, not in detail in this, later used to detail the record it.
⑤ complement: The application of the complement is widespread, the computer's internal operation is to use the complement.
The complement can also be obtained from the original code:
For positive numbers: complement is the complement;
For negative numbers: the symbol bit of the original code is unchanged, the remaining bits are reversed, and the whole plus one gets the complement.
For example: The original binary code is 10010, the sign bit 1 is unchanged, the remaining 0010, the inverse 1101; integration is 11101, plus one gets 11110. That is, the original code is 10010 of the complement of 11110.
• Complement 0, assuming that there are 4 bits, then for 0, the original code is 1000, take the inverse plus one, get 0000 (assuming a bit width fixed); For +0, the complement is 0000, so the complement of 0 has only one form of expression.
The range of values represented by • N is: -2^ (n-1) ~+ (2^ (n-1)-1)
3. binary Binary Add and Subtract operation and Overflow phenomena
(1) the addition and subtraction of binary operations
① for the general unsigned operation, the direct operation is good, there is nothing to say. But inside the computer, it is a complement operation, there is a place to record, complement the addition and subtraction operations for the other signed number, it seems relatively simple. In addition, subtraction can be added as an addition, minus a number can be counted as a negative number.
② for the addition of complement: complement + complement = complement. Of course, this is a certain condition, the following examples to explore.
③ Example one: (+3) + (+4), the result is obviously +7, the binary complement is the 0011+0100, the result is 0111, it is obvious that 0111 is the complement of +7.
Example two: (-2)-6 = (-2) + (-6), the result is obviously-8; the operation with twos complement is 1110 + 1010, which is 11000, which is obviously--8 of the complement.
Example three: +6-3 = (+6) + (-3), the result is obviously +3, with the binary complement of the operation, it is 0110+1101, the result is 10011, ah, this is obviously not +3 of the complement, but, when we remove the highest bit, only a four-bit 0011, this is obviously +3 of the complement.
Example four: +4-7 = (+4) + (-7), the result is obviously-3; the binary complement operation is 0100+1001, the result is 1101, the result is obviously-3 of the complement.
(2) overflow phenomenon
① through the above four examples, introduced the concept of overflow: if the addition of a few out of the number of defined scope, it is said that an overflow occurred. Two different numbers add up, certainly do not overflow; the addition of the same number may overflow.
② judgment Overflow rule: If the addend symbol is the same, and the ampersand is different from the addend symbol, there is an addition overflow.
③ Four more examples of the previous one:
In general, in cases where the bit width is fixed,
Example one adds, the result is 4 bits, no overflow, and the results are correct.
Example two, although the result is correct, but the result is 5-bit, if the highest bit truncated, then the result will be wrong, that is, overflow generated. Here also reminds us, for the same number added, to add one more, in fact, is to add a bit more than one sign.
Example three results although is incorrect, but this is the result of 5 bits, we intercept the highest bit, the result is correct, so this does not overflow, that is, the result (four) is still right.
Example four, no overflow.
(a) the numbering