# Include <iostream>
# Include <stdio. h>
# Include <string. h>
# Include <conio. h>
Using namespace STD;
Int main ()
{
Float a = 1.0f;
Cout <sizeof (INT) <Endl; // 4
Cout <sizeof (float) <Endl; // 4
Cout <(INT) A <Endl; // 1
Cout <& A <Endl;/* obtain the address of a in hexadecimal format 0012ff7c */
Cout <(INT) & A <Endl;/* (INT) & A: forcibly convert the address of a to a decimal integer of 1245052 */
Cout <(Int &) A <Endl;
/* (Int &) A: forcibly converts a's reference to an integer, which indicates the memory of A. It is defined as the float type and is initially 1.0f,
But now I want to explain this memory by INT type (that is to say, the data in the memory address where A is located is originally represented by float type storage, and you have to explain it by INT type ).
1.0f storage in memory is
0 011 1111 1 000 0000 0000 0000 0000 0000.
He interpreted the integer as 2 ^ 29 + 2 ^ 28 + 2 ^ 27 + 2 ^ 26 + 2 ^ 25 + 2 ^ 24 + 2 ^ 23 = 1065353216
(Int &) A is equivalent
* (Int *) &
* (Int *) (&)
* (Int *) &)
*/
Cout <boolalpha <(INT) A = (Int &) a) <Endl; // output false. Because 1! = 1065353216.
Float B = 0.0f;
Cout <(INT) B <Endl; // 0
Cout <& B <Endl;/* obtain the address of B in hexadecimal format 0012ff78 */
Cout <(Int &) B <Endl; // 0
Cout <boolalpha <(INT) B = (Int &) B) <Endl; // outputs true because 0 = 0;
/*
(Int &) A directly obtains the value of A in the memory unit (Address) without conversion.
(INT) the value of a in the memory is converted to the int type.
The float type is stored in the memory in the form of the symbol bit index ending number.
Standard from 754: The order code uses an additional code (the reverse symbol of the complement code), and the ending number uses the original code.
So the 1.0f format in the memory is
0011 1111 1000 0000 0000 0000 0000
Therefore, 0x3f800000 is output.
0 memory storage format
0000 0000 0000 0000 0000 0000 0000
*/
Return 0;
}
======================================How is float expressed in C language?======================================
How is float represented in C language? How to arrange the order code on 32 bits, that is, which bit is
Tail number, which is the level code, and that is the symbol bit. I heard it is related to CPU. Is it true?
In C ++, the real number (float) is stored in four bytes, namely, 32-bit binary bits. Where
It has one-digit sign, eight-digit exponent, and 23-digit valid digits. In fact, the valid digit is 24, because
A valid number is always "1" and does not need to be stored.
A valid digit is a pure decimal number. The first digit in the eight-digit index is the symbol bit.
Generally, the symbol bits are different. "1" indicates positive, and "0" indicates negative. The symbol bit of the entire real number uses"
1 "indicates negative, and" 0 "indicates positive.
In the four bytes that store the real number, the highest bit number of the highest address byte is 31, the lowest address
If the byte's percentile number is 0, the distribution of each part of the real number among the 32 binary digits is as follows: 3
1 is a real number symbol bit, 30 is an exponential symbol bit, 29---23 is an exponential bit, and 22---0 is a valid number
Bit. Note that the first valid number does not appear in the memory. It is always "1 ".
To convert a real number to the C ++ real number Storage Format, follow these steps:
(1) first convert the absolute value of the real number to the binary format. Pay attention to the integer and decimal parts of the real number.
The binary method is different.
(2) shifts the decimal point of the binary real number to the left or right until the decimal point is moved to
The right of a valid number.
(3) Place the numbers from the first digit on the right of the decimal point to 22nd to 0th digits.
(4) If the real number is positive, put "0" in the 31st bits; otherwise, put "1 ".
(5) If n is obtained from the Left shift, it indicates that the exponent is positive, and the 30th bits are placed in "1 ". If n is right
If the obtained value is or n = 0, the 30th bits are placed in "0 ".
(6) If n is obtained from the Left shift, subtract n and convert it to binary, and add "0" to the left"
Make up seven digits and place them in 29th to 23rd digits. If n is obtained from the right shift or n = 0, convert n to binary
Add "0" on the left to fill up the seven digits, and then you will reverse the request, and then put the 29th to 23rd digits.
You can convert the real number format stored in a computer to the standard decimal format as follows:
(1) write the binary numbers from 22nd to 0th bits, and add a "1" to the leftmost side to get 20
Four valid digits. Place the decimal point on the right of the leftmost "1.
(2) obtain the value n represented by 29th to 23rd bits. When the value of 30 digits is "0", we will reverse all N. When 30
When the bit is "1", n is increased by 1.
(3) shifts the decimal point N places to the left (when the 30 digits are "0") or N places to the right (when the 30 digits are "1)
To obtain a real number in binary format.
(4) convert the binary real number into a decimal number, and add a positive value based on whether the 31st bits are "0" or "1 ".
Or minus sign.
In particular, the real number 0 is expressed as 0000000000000000000000000000000 In the float format of C ++.
0.
If you still don't understand it, here are a few examples.
I. Convert 23.56 to the float format of C ++.
(1) convert 23.56 to binary and then it is about "10111.1000111101011100001 ".
(2) move the decimal point four places to the left to get "1.01111000111101011100001 ".
(3) there are 24 valid digits. Remove the leftmost digit "1" and get "0111 ".
1000111101011100001 ". Put it in 22nd to 0th bits.
(4) Because 23.56 is a positive number, put "0" in 31st bits ".
(5) Because we move the decimal point left, we put "1" in the 30th bits ".
(6) Because we shift the decimal point to four places to the left, we subtract 4 from 1 to 3 and convert it into binary and Complement
Seven digits get 0000011, And put 29th to 23rd digits.
Complete.
If the leftmost is set to 31st bits and rightmost is set to 0th bits, in C ++, float Format 2
3.56 indicates: 01000001101111000111101011100001. Correspondingly,-23.56 is
Sample: 11000001101111000111101011100001.
II. Convert the real number 0.2356 to the float format of C ++.
(1) convert 0.2356 to binary and then about 0.00111100010100000100100000.
(2) Move the three digits to the right of the decimal point to 1.11100010100000100100000.
(3) extract 23 valid digits from the right of the decimal point, that is, put 11100010100000100100000
The value ranges from 22nd to 0th.
(4) Because 0.2356 is positive, put "0" in the 31st bits ".
(5) Because we shifted the decimal point to the right, we put the decimal point "0" in the 30th-bit format ".
(6) because the decimal point is shifted three places to the right, convert the decimal point to binary, and add "0" to the left to fill in the seven
BITs, get 0000011, get 1111100, put 29th to 23rd bits.
Complete. Therefore, 0.2356 is represented in the float format of C ++: 00111110011100010100000100
100000. The leftmost and rightmost are 31st digits.
3. Convert the real number 1.0 to the float format of C ++.
(1) convert 1.0 to binary and then 1.00000000000000000000000.
(2) You do not need to move the decimal point. This is the case where we say n = 0 in the conversion method.
(3) Place the 23-digit valid number on the right of the decimal point 00000000000000000000000 into 22nd.
To 0th bits.
(4) Because 1.0 is positive, put "0" in the 31st bits ".
(5) Because n = 0, put "0" in the 30th bits ".
(6) Because n = 0, 0000000 is obtained for the first seven digits of 0.
29th to 23rd bits.
Complete. So the real number 1.0 is represented in the float format of C ++: 0011111110000000000000000
0000000. The leftmost and rightmost are 31st digits.
This is an IEEE short real number format and is suitable for x86cpu.