(INT) A, & A, (INT) & A, (Int &) a difference

Source: Internet
Author: User

# 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 float is represented in C ============================

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. Note that the integer and decimal parts of the real number are different in binary format.
(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 obtained from the right shift 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. Then add "0" to the left to fill in the seven digits and place the 29th to 23rd digits. If n is obtained from the right shift or n = 0, convert n to binary and add "0" to the left to fill up the seven digits. Then, you can reverse the request and place the values between 29th and 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 between 22nd and 0th bits, and add a "1" to the leftmost side to get the twenty-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 the value of 30 digits is "1", n is increased by 1.
(3) shifts the decimal point N places to the left (when the 30 digits are "0") or shifts n places to the right (when the 30 digits are "1") to obtain a real number in binary representation.
(4) convert the binary real number into a decimal number, and add a positive or negative number based on whether the 31st bits are "0" or "1.

In particular, the real number 0 is expressed as 00000000000000000000000000000000 In the float format of C ++.
 

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 already twenty-four valid numbers. Remove the leftmost "1" and get "01111000111101011100001 ". 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 want to shift the decimal point to four places to the left, we subtract 4 from 1 to 3 and convert it into binary, and fill in the seven digits to get 0000011, And put 29th to 23rd digits.
Complete.
If the leftmost is set to 31st bits and rightmost is set to 0th bits, the float format 23.56 in C ++ is represented as follows: 01000001101111000111101011100001. Correspondingly,-23.56 is represented as follows: 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) take 23 valid digits from the right of the decimal point, that is, 11100010100000100100000 to 22nd digits.
(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. Fill in "0" on the left to make up seven digits and get 0000011. You can get the inverse and get 1111100, put 29th to 23rd bits.
Complete. Therefore, 0.2356 is represented in the float format of C ++: 00111110011100010100000100100000. 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 00000000000000000000000 to 22nd digits on the right of the decimal point.
(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 7-digit complement of 0, and 1111111 is obtained for reverse lookup.
Complete. Therefore, the float format of the real number 1.0 in C ++ is 00111111100000000000000000000000. The leftmost and rightmost are 31st digits.

From: http://hi.baidu.com/jrwen0/item/37004214e30aba8989a9561a

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.