Section III representation and processing of information
A. Information Store
1, bytes
The smallest addressable memory unit, each byte is identified by a unique number, called an address.
2, the representation and transformation of the system
① decimal to Binary method is: the decimal number in addition to the 2 method, that is, the decimal number in addition to 2, the remainder of the number on the throne, the quotient value continues to divide by 2, according to this step continues downward operation until the quotient is 0.
② binary Decimal method is: The binary number by the right expansion, add the decimal number.
The ③ binary to octal method is: 3-bit binary number is expanded by right and added to get 1-bit octal number. (Note that 3-bit binary to octal is converted from right to left and 0 when insufficient).
④ octal to binary method is: octal number by the addition of 2 to take the remainder method, the binary number is obtained, for each octal is 3 binary, when insufficient in the leftmost 0.
⑤ binary Turn hex
The ⑥ method is: Similar to the binary octal method, octal is taken in the Triad, hexadecimal is four in one. (Note that 4-bit binary to 16 binary is converted from right to left, 0 when insufficient).
⑦ Hex Turn Binary
The ⑧ method is: hexadecimal number by the addition of 2 to get the remainder method, the binary number is obtained, the hexadecimal is 4 binary, when insufficient in the leftmost 0.
⑨ decimal to octal or hexadecimal according to the addition of 8 or 16 to the remainder, until the quotient is 0.
3. Boolean algebra
The Boolean algebra number originates from the field of mathematics and is a formula for set and logical operations: 〈b,∨,∧,¬〉. where B is a non-empty set, ∨,∧ is the two two-dollar operation defined on B, which is a unary operation defined on B.
The set operation through Boolean algebra can obtain the intersection, the set or the complement between different sets, and the logical operation can be made to the different sets with, or, not.
Basic rules:
1. A+b=b+a, a b=b a.
2. A. (b+c) =a b+a C,
A + (b c) = (a+b) · (A+c).
3. A+0=a, a 1=a.
4. A+a′=1, a a′=0.
4. Complement
The most common form of a computer representation of a signed number.
Complement of positive number = original code
The complement of the negative number = The original code you take back plus 1
The most significant bit is also called the sign bit.
5. Addressing and byte order
Small End method: the least significant byte at the front, "high to high, low to Low", is the rule used by most Intel compatible machines, including IBM and Sun's personal Intel-compatible processors.
For example: the variable x is int, at address 0x100, and the hexadecimal value is 0x01234567.
Big-endian Law: Address: 0x100 0x101 0x102 0x103
67 45 23 01
In 0x01234567, the high byte is 0x01 and the low byte is 0x67.
Second, integer representation
1. Conversion between signed and unsigned numbers
For most C language implementations, the general rule for converting between signed and unsigned numbers with the same length is:
The value may change, but the bit mode will not change.
The relationship between a signed number x and the corresponding unsigned number T2UW (x):
T2UW (x) = X+2^w, x<0
X, x>=0
2. Bit representation of extended numbers
0 Extension: Add 0 at the beginning. (Converts an unsigned number to a larger data type)
Symbol extension: Adds a copy of the value of the most significant bit. (A complementary number is converted into a larger data type)
3. Truncating numbers
May change the value--a form of overflow.
For unsigned digit x, truncated to K-bit, equivalent to calculation: X mod 2 (k). (K Power)
4. Signed and unsigned numbers in the C language
When the C language performs an operation, if one operand is signed and the other operand is unsigned, the C language implicitly converts the signed strong to unsigned and assumes that both are non-negative and perform the operation.
PS: To make a negative number equal to a positive number, you can let the negative number and this positive number of the unsigned form equal, and then in contrast, one of the two strong to unsigned form, you can.
5. Extend the bit representation of a number
A common operation is to convert between integers of different lengths, while keeping the values constant. Of course, when the target data type is too small to represent the desired value, this may not be possible at all. However, it should always be possible to convert from a smaller data type to a larger type. To convert an unsigned number to a larger data type, we simply add 0 to the beginning of the representation. This operation is called a 0 extension (zero extension). To convert a binary complement number to a larger data type, the rule is to perform a symbol extension (sign extension) and add the value of the most significant bit in the representation.
Three, integer arithmetic
1. Complement addition
complement addition
[X+y] complement = [X] complement + [Y] Complement
Note: Because the bit length of the operator in the computer is fixed (constant-length operation), the highest bit of rounding generated in the above operation will be discarded, so the result is not 100001010, but 00001010.
2. Complement subtraction
[XY] complement = [x] Complement-[Y] complement = [x] complement + [-y] complement "1"
3. Complement multiplication
Complement multiplication does not have the property of "x*y" complement = "X" complement X "Y" complement. But "x*y" fill = = "X" complement XY, the result is complementary, such as x=101,y=011,[x*y] complement =-[(-101) *011]=-[011*011]=-01001=10111
4. Multiply constants
In machine operations, multiplication is always slow, while addition and displacement (left shift) are relatively fast. So in the compiler, a combination of shift and addition operations is used instead of multiplying the constant factor. This method is applicable for both unsigned and complementary operations.
When the constant is 2, the K-power is shifted directly to the left K-bit.
When a constant is not an integer power of 2, the constant c is represented as a sum of several integral powers of 2, combined with shift operations and addition operations.
5, divided by the power of 2
In machine operations, the multiplication is slower than Fabienne. When the divisor is an integer power of 2, it is resolved by moving right. When you move right, you need to differentiate between unsigned and complementary.
Four, floating point number
1. Binary decimals
Fixed-point notation: "." For the bounds (cannot effectively represent a large number)
Decimal: The right of the number to the left of the decimal point is a non-negative power of 10, the integer value is obtained, the right number is the negative power of 10, and the decimal value is obtained.
Binary: The right of the number to the left of the decimal point is a non-negative power of 2, and the right of the number is the negative power of 2.
2.IEEE floating point representation
To represent a number in the form of V = ( -1) s * M * 2E:
Symbol: s Determines whether the number is negative (s = 1) or positive (s = 0), while the symbolic bit interpretation for the value 0 is handled as a special case.
Mantissa: M is a binary decimal that has a range of 1 to 2-ε, or 0 to 1-ε.
Order code: The role of E is weighted to the floating point data, which is 2 of the power of the E (possibly negative). Divide the bit representation of a floating-point number into three fields and encode the values:
A separate sign bit s directly encodes the symbol S.
The Order field of K-bit exp = EK-1...E1E0 encoded Order E.
The N-bit decimal segment Frac = fn-1...f1f0 encodes the mantissa m, but the encoded value also depends on whether the value of the Order field equals 0.
3. Two common formats
The single-precision floating-point format of the C language float and double-precision floating-point format double.
In float, the S, exp, and Frac fields are 1 bits, k = 8 bits, and n = 23 bits respectively, resulting in a 32-bit representation;
In a double, the S, exp, and Frac fields are 1 bits, k = 11 bits, and n = 52 bits, respectively, and a 64-bit representation is obtained.
Encounter problems and solve
Why a negative overflow is generated
Answer: Through the example of Baidu and reference books know, and positive overflow, as long as the number of bytes above the data, it will overflow, just positive overflow is positive, negative overflow is negative value beyond the lower limit.
Section III Representation and processing of information