Bitwise conversions -- % F % d problems during int-type double Conversion

Source: Internet
Author: User

For such a program output result

# Include <iostream>
Using namespace STD;
Int main ()
{
Printf ("% F", 5 );
Printf ("% d", 5.01 );
}

My analysis is as follows: first of all, note that the numbers in the stack are stored from high to high! For example, int A = 3; & A indicates that the low address is not a high address, that is, the end of the int rather than the starting position of the Int .)

In fact, when we figure out the storage format of floating point numbers, we will understand the mysteries. In the C language standard, floating point numbers use the ieee754 standard float data storage format as follows:

The highest percentile symbol is the end number of the S-level code E. M is the highest 31 bits. Save the symbol S ", 0" to indicate a positive number, and 1 to indicate a negative number of 30 bits ~ 23 bits, a total of 8 bits, the shift code method (exponent value plus offset 127) saves the index part, called the level code 22 Bits ~ 0 bits, 23 bits in total. The retention coefficient is called the ending number. For the normalized binary number, the leading "1" of the entire digit is not saved.

Implicit direct saving of decimal part for double type, IEEE 754-1985 standard uses 64-bit representation, details: · maximum 63 bits, save s ", 0" positive number ", 1 "negative number;-62-bit ~ 52 bits, a total of 11 bits, the shift code method (adding a value of 1023) to save the index, called the order code;

· 51-bit ~ 0 bits, 52 bits in total, saving the coefficient part, called the ending number. For the normalized binary number, the "1" of the entire digit does not save the implicit value), directly Save the fractional part and press 5.01 into the stack, it is the double number, and the stack is 8 bytes.

In addition, % F is output by double.

For printf ("% F", 5), first, the int type occupies 4B in the memory, and the output is indeed 8B bytes, 5 In the memory, 0000 0000 0000 0000 0000 0000 0000 0101% F output must also be added to 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0101, because the double class has only 6 digits after the decimal point, it is impossible to get 101 Where there are non-0 digits, so the answer is 0.000000.

For printf ("% d", 5.01) Double Type 5.01 occupies the memory 8B, while % d only takes the high 4B, because the high 4B stores the symbol bit and level code, 5.01 when the memory is stored in decimal places, it must be 0.501000. Therefore, the real level code bit is not 0. Here, it refers to the level code after the final conversion, rather than the shift, it refers to the number we really need. What we need is a low 4B number. Because the low 4B stores the binary representation of decimal 0.501000, but it is converted to an integer when output by % d, the output is 1889985710

I don't know if I understand it like this, right? Well, it would be nice if I had learned the compilation and compilation principles, so that I can see the bottom layer at a glance!

I hope you can correct your criticism !!!!


 

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.