C language entry-03

Source: Internet
Author: User

I have already written to Article 3 without knowing it. Let's first review what knowledge points have been analyzed in article 02. We have introduced the PE Structure of the program composition structure. Here we will expand our knowledge, on the LINUX platform, there is also a file format similar to the PE structure. It is also an extension of COFF, called ELF. If you are interested, you can study it. We also introduced how to allocate memory, ing address space, and logical address space for the hello world system, stack (people generally like to call it a stack), heap, code zone, and data zone. We have also analyzed the approximate scope of these regions in VC6, which gives you a simple understanding of the program.


Today I think about it. we mainly discuss the problems in 01. In 01, we mainly discuss the representation of Integers ([unsigned] (char, short, int) in memory, today, let's do another exercise, then point the direction to the floating point, and analyze the representation of the floating point in the memory, and why there is a loss of precision in the representation of the floating point. These issues are not important in themselves, it is important to understand the process.


Review: specify two int integers, 51245412 and-6231541, and write the memory representation. Assume that the starting address is 0X0012FF7C.


1. Calculate the binary representation of 51245412. If it is not 32 bits, add 0 to the left (You know). You can use a calculator for this step.

51245412 0000 0011 0000 1101 1111 0001 0110

Then the corresponding hexadecimal format is written.

0000 0011 0000 1101 1111 0001 0110
0 3 0 d f 1 6 4

We can get its hexadecimal representation: 03 0D F1 64, and then write it to the address according to the principle of high, low, and low

Memory Address Value
0X0012FF7C 64
0X0012FF7D F1
0X0012FF7E 0D
0X0012FF7F 03

View the actual results


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192P03529-0.jpg "title =" BMP 1.bmp "alt =" 152600704.jpg"/>


Note: We didn't press F9 to break down the breakpoint. Instead, we embedded a piece of assembly code, int 3. In fact, it is a soft interrupt. When the CPU is executed here, it will be broken by itself, let's use F9 for a reason. The main purpose of writing this article is to let everyone know more. We can see that the black part in the figure is exactly the same as that calculated by ourselves.


2.-6231541 the number is no longer a positive number, but a negative number. We need to calculate the binary form of the corresponding positive number first, and then add 1 to the bitwise result to obtain the final result.

6231541 0000 0000 0101 1111 0001 0101 1111

Bitwise Inversion

0000 0000 0101 1111 0001 0101 1111
1111 1111 1010 0000 1110 1010 0000

The result is

1111 1111 1010 0000 1110 1010 0000

Write as hexadecimal

1111 1111 1010 0000 1110 1010 0000
F a 0 e a 0 B

FF A0 EA 0B is our result.


Memory Address Value
0X0012FF7C 0B
0X0012FF7D EA
0X0012FF7E A0
0X0012FF7F FF

View actual results


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192P0B34-1.jpg "title =" BMP 2.bmp "alt =" 152631868.jpg"/>


Now we have reviewed the complement code again. Let's look at the floating point representation.


In fact, floating point numbers can also represent binary. This is a must .. Haha, for example, 0.5 indicates that the binary value is 0.1.


1 before decimal point represents 2 ^ n, for example, 11.01 from the left, 1st 1 represents 1*2 ^ 1, and 2nd from the left 1 represents 1*2 ^ 0, the rightmost 1 represents 1*2 ^ (-2), and its sum is 2 + 1 + 0.25 = 3.25. This should not be hard to understand. Now, for example, we have an integer of 19.375. We want to convert it to binary. First, we need to convert it to the integer on the left, 16 + 2 + 1 = 19, so it is 10011, so how can we turn it on the right? If you have a foundation, we all know the methods except 2 and multiplication 2. We will not introduce those methods. If you are good at mathematics, 0.375 you should know how much it is, 3/8 to simplify it, 3/8 = 1/8 + 2/8 = 1/8 + 1/4 = 2 ^ (-3) + 2 ^ (-2 ), I understand. I think 0.375 has my intention. Haha, you are fooled. Therefore, the complete binary representation should be 10011.011. If you do not understand this, you have to take a good look at primary school mathematics. We already have binary, so the first thing we need to do is to convert it to 1. xxxx * 2 ^ y format, just like 15.62 = 1.562*10 ^ 1, 15642.125 = 1.5642125*10 ^ 4. It's only in binary format, the above result can be converted to 1.0011011*2 ^ 4. The result is actually 19.375, but it is an exponential binary representation.


Now we have another representation equivalent to 19.375 in the form of 1.0011011*2 ^ 4, and we say that the memory representation of floating point numbers also starts from here, the floating point number indicates that it is not a supplementary code, but a transfer code. Rules

The highest sign bit. If it is 1, it indicates a negative number. If it is 0, it indicates a positive number.

The 8-digit index on the right of the highest digit, plus or minus the left

Other 23-digit numeric digits

The above table describes the float data representation. The symbol bit is well determined. As shown above, our 19.375 is a positive number, so the floating number is 0. How can we determine the index bit? It is based on 127. We are converting binary to 1. xxxx * 2 ^ y: the decimal point is shifted, and the y bit is moved. If it is left shifted, 127 + y is the index value. If it is right shifted, then we use the value of 127-y to obtain the index bit. Because we shifted left, the index bit is 127 + 4 = 131, And the binary format is 10000011, in this way, we have determined the first nine digits. The last 23 digits indicate that the number is 0011011 on the right of the decimal point. You will say that it is not 23 digits long, add 0 to the right, and add 0 to the right without changing the size of the original number .. The following table is used together.

Symbol bit Exponential bit Numeric bit
0 10000011 0011011 0000 0000 0000 0000

Now, we can connect them and combine them into hexadecimal data, just as we did when we were doing integers.

0100 0001 1001 1011 0000 0000 0000
4 1 9 B 0 0 0 0

Consolidate data: 41 9B 00 00 write memory space

Memory Address Value
0X0012FF7C 00
0X0012FF7D 00
0X0012FF7E 9B
0X0012FF7F 41

It's time to verify the miracle


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192P02046-2.jpg "title =" BMP 3.bmp "alt =" 152701842.jpg"/>


Amazing.


If it is changed to a negative number, can you directly write the value of it? Based on the above value, it is actually very simple. Do we just need to set the highest position to 1, so the original value of 4 should be 1100, that is, C. Verify the miracle again.


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192P010E-3.jpg "title =" BMP 4.bmp "alt =" 152726562.jpg"/>


Is it EASY.


If you have understood the preceding example, you only need to modify the double data in a few places. double occupies 8B, 64b, and the highest bit is 1, the index is not 8b, but 11 digits, and the base 127 is not 127 (2 ^ (8-1)-1), but 1023 (2 ^ (11-1) -1), the remaining 52b is a numerical bit. I hope you can give several examples to do more tests.


Well, there are so many basic introductions. The problem is that we still have a problem to solve, that is, the Qinghai-Tibet Plateau. Oh, my god. Wrong, that is, the loss of floating point precision.


Why is there a loss?


1. If the binary value of a floating point is 1. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx * 2 ^ y. Here, the number of digits of x exceeds 23. At this time, the value bit is truncated, and only the first 23 digits are taken, of course, precision will be lost. This problem can be solved using double, but if the number of digits of x exceeds 52, double is powerless.


2. floating point numbers cannot be converted into binary numbers. The decimal part of the number is 0.375 because it can be expressed as 0.011, but in fact there is still some data that cannot be accurately expressed, for example, 0.2, If we perform the multiplication of 2 to take an integer operation, we will find that we enter the loop and cannot get an accurate representation. This is also the cause of the loss.


By analyzing the causes of loss of precision, we can know that the precision of double is higher than that of float and why.


Now, this article is here for the time being. In the next article, we will start to talk about the data type. It is very important. It should be said that it is super important.


This article from the "Qian song" blog, please be sure to keep this source http://qianqianquege.blog.51cto.com/8004200/1304575

Related Article

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.