Task Description: computer programming summarized various data types, the representation and calculation methods. Focus type conversion, numeric overflow and shift operations.
![](https://img-blog.csdnimg.cn/20200216115413339.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTgyMTMxNw==,size_16,color_FFFFFF,t_70)
A, int
1, int-type data
int-type data occupies 4 bytes (32 bits) in the memory, seven significant digits, the values range -2 ^ 31 to 2 ^ 31-1. (Signed)
unsigned int in the range of 0 to 2 ^ 32
- display method
All bits occupied int type, left bit first (i.e. most significant bit) is the sign bit. The sign bit of type int, 0 for positive, 1 for negative. In the 32-bit operating system, the back rest 31 is a numerical value bits.
- Calculation
Calculation complement
2, short Data
short type byte data occupies no more than the number of bytes int type, represented by 2 bytes (16 bits).
3, long Data
Data represent long (32-bit operating systems) using four bytes (32 bits)
4, integer overflow
For unsigned integer overflow, the overflow will be the number (8 * sizeof (type)) ^ 2 modulo operation do
i.e. if an unsigned char (8bit) overflows, the overflow will be the value of the modulo modulo 256, for example:
signed char x=0xff;
printf("%d\n",++x); //输出0
/*因为 0xff+1 =256 ,256与256求模取余结果为0*/
For signed integer overflow, the result is the actual result of calculation
e.g.
signed char x=0x7f;
printf("%d\n",++x); //输出-128
/*因为 0x7f+1 =0x80即-128*/
5, integer shift
Unsigned shifting the number of logical operations are performed by shifting 1
- left << (shift left logical) 1
Discarding the most significant bit, the least significant bit 0 complement
signed number : When the leftmost symbol shifted out, displaced overflow sign of the value may change.
Special case: when the maximum number of bits bit left shift count exceeds this value type, the compiler will use the number of bits left to die maximum number of bits of the type, and then take the remainder
- >> right shift (arithmetic right shift) 2
Signed numbers : the sign bit to the right, 0 complement positive, negative S.1.
Second, the floating-point data - data with a decimal value or index
1, float type
float type occupies 4 bytes (32 bits) in the memory, seven significant digits, the values range -3.40E + 38 ~ 3.40E + 38
- display method
The step of converting a float type memory storage format as follows:
(1) the first real number absolute value to a binary format , attention integer part and fractional part of a binary method of real numbers.
(2) the format of the real number binary decimal point to the left or right by n bits , until the decimal point is moved to the right of the first significant digit.
(3) from the decimal point first start out on the right twenty-three digit number into 22 to bit 0 , the default system removes 1 before the decimal point.
(4) If the real number is positive , then in position 31 into "0", or placed in "a."
(5) if n is left obtained , indicating index is positive, into position 30 "a."
If n is obtained right or n = 0, into the position 30 "0."
(6) if n is obtained left, then to a binary 1 after subtracting n, and on the left by "0" complement seven, put the first 23 to 29. If n is right to give or n = 0, plus the left post into a binary "0" to make up seven, then you negate, then add 29 to the first 23 will be n.
For example: 11.9 memory storage format
(1) into the binary is about 11.9 "1011. The
1110011001100110011001100 ...."
(2) decimal point to the left three bits to the right of the first valid: "11100110011001100110 1.011." 24 significant digits to ensure the right taken excess (error produced here).
(3) which is already there are twenty four significant digits, the leftmost digit "1" removed to give "011 11100110011001100110
" a total of 23bit. 22 to put it on the first float 0 storage structure.
(4) Since 11.9 is positive, and therefore into the "0" at the 31 real symbols of bits.
(5) Since we put the decimal point to the left, so the 30-bit exponent sign bit into a "1."
(6) Since we are to the left of the decimal point three, so will have to subtract 1 2 3, into a binary, and made up 7 0000010 get placed 29th to 23rd.
Finally, as represented 11.9: 010000010011 11100110011001100110
- Calculation
11.9f-calculated 12.0f
12.0f memory storage format is: 010000010 10000000000000000000000
11.9f memory storage format is: 01111100110011001100110 010000010 visible exponent of two numbers are identical, as long as the significant digits of the subtraction can.
12.0f-11.9f Results: 010000010 00000011001100110011010
result as decimal reduction: 0.000 = 0.10000038 11001100110011010
Similar double-data case , but its order code is 11, the offset of 1023, the mantissa is 52 bits.
2、double型
double-data occupies 8 bytes (64 bits) in the memory 15 significant digits, the values range -1.79E + 308 ~ -1.79E + 308.
Program, written in floating-point format is generally
sign integer part. Exponent part fractional part
- The integer and fractional parts can be optionally, but not are not
- Decimal point and exponent can not do without at the same time
- Exponent portion e is a letter or beginning with E, followed by an integer
3, floating-point overflow
When the floating-point overflow , was given an infinite value, printf function shows inf.
When the floating-point underflow , the index has reached a minimum part, the computer had to mantissa part right, binary vacated in the first place, dropped the last bit binary number.
In decimal, for example, to a number of four significant figures (e.g., 0.1234E-10) divided by 10, the result is 0.123E-10. While the results are obtained, but in the calculation process has effectively lost the original tail numbers on the site. Overflow (underflow) *** *** this case is called.
The loss of the C language type floating-point value *** full precision floating point value called *** less than normal. Thus, the smallest positive floating-point number is divided by 2 to obtain a value lower than the normal. If divided by a very large value will cause all bits are 0.
4, the shift operation
C language does not support floating point shift operation
Third, the character data
1, char type
char type data occupies 1 byte (8 bits) in the memory
for character data representing the interior of ASCII characters is
an integer of ASCII characters can be regarded as 0 or -128 to 127 to 255
- Representation
In the C language, is actually character data in memory is stored in binary form.
- Calculation
Character data at the time of addition and subtraction of the phase, the system will first char data type invisible manner then converted to an int phase addition and subtraction.
Fourth, the type conversion
1, automatic type conversions
All data in the mixing operation of various types, the compiler will automatically convert data types will be converted to participate in the operation of the same type, and then calculated. Conversion rules are as follows:
- Data conversion carried out in longitudinal direction of increasing value to ensure that no distortion or reduced accuracy. For example, when involved in computing and long int, int first type of data transfer operation performed as a long before.
- All floating point operations are performed in double precision, even if only float type operation, it must first be converted to double type, in order to carry out operations.
- char and short time involved in computing, must be converted to type int.
2, cast
Cast programmer explicitly mentioned, one type of code needs to be indicated by a specific format conversion. In other words, the programmer does not need intervention of the automatic type conversions, casts programmer must intervene.
Logical shift left, the highest bit lost, the lowest fill 0;
logical shift right, make up the most significant bit 0, the least significant bit is lost; ↩︎ ↩︎When the arithmetic shift left, turn left through the tail 0s, the highest symbol remain unchanged.
When the arithmetic right, turn right one, the loss of the tail, after the sign bit to the right, a sign bit copy of the original position; ↩︎