how int-type integers are stored in memory

Source: Internet
Author: User
The way int type is in memory

Cause: In other words, the landlord writes a function that converts the string type to an int type, which is to rewrite the atoi () function, and when considering the overflow, write a line of code:

if (. MINUS&&NUM>0X7FFFFFFF) | | (MINUS&&NUM<FFFFFFFF)) {
    num=0;
    break; 
Note: bool minus is to determine whether the number is positive or negative.

When the test was found to enter a negative number of strings, the result is actually 0.
My keen excavation, must be num=0 this sentence has played a role, but ordinarily should not enter this if statement ah.
So I began to analyze, the type of int is a signed type, that is, the first is the sign bit, when the sign bit is 0 when the positive number, when the sign bit is 1 when the negative number, then I entered the correct Ah, 0xFFFFFFFF should not be the decimal number 2147483647 this number of negative mode-2147483647 Mody ...
The landlord puzzled, so began to experiment.

    int i=0xffffffff;
    cout << i;
} Output results:-1

I was shocked, obviously should be so many digits of negative, and so many f give me a-1, I really want to ask the computer what you think. Then I looked at the book, and the same judgment in the book was
minus&&num<80000000
So I got some code down here.

    int i=0x80000000;
    cout << i;
} Output results:-2147483648

Well, it seems that the book is right, do not know how the computer is thinking how to translate.
So I began to seriously think about this problem, computer is how to think, the invention of C + + language is how people think, and why.
When I was at school, I vaguely remember that it was related to addition and subtraction.
So he took out a paper pen and started to simulate.
First I add int 1 and int-1, that is 0x0000001+0xffffffff, eh, Miracle came, the result is 0x100000000, we are optimistic about 9 16, and according to the int type of storage space should be 8 16, that is 4 bytes, so
There's 0x00000000 left, which is not exactly 0. Then the landlord tiger body an earthquake, continue to test.
If it's int 1 minus int-1.
That's 0x00000001-0xffffffff. So the landlord Lenovo above example, decided to fill 1 digits in front of the integer, that is, the result of 0X100000001-0XFFFFFFFF is just 0x00000002. So I took, the people who invented the person think so much more than we do ah, you want to ah, if it is so do not have to convert the symbol, ah, otherwise, if I understand before,-1 is 0x80000001, 1+ (-1) should be first to convert 1 to +1, and then add that is 0x00000001+ 0x00000001=0x00000002, such words are too cumbersome to handle, and the process of dealing with positive and negative numbers has a significant effect on efficiency.

All right, the analysis above is finished, and then I'll talk about other development experiences.
1. If you see a similar 0xfffffffa how to judge it is a negative number. It's simple, just add a minus sign with the 0x100000000-0xfffffffa number.
2. If you want an int integer to be reversed directly using the symbol "~" and then +1, this method is more efficient than using the symbol block, because it is a bit operation, which is faster than operator operation.
such as: a=~a+1

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.