The data range of computer and mathematics

Source: Internet
Author: User

Nonsense don't say, first a program, you boys and girls to guess the output of this program:

#include <stdio.h>
#define MAX 255

int main (int argc, char **argv) {
unsigned char I, A[max];
for (i = 0; I <= MAX; i++) {
A[i] = i;
}
for (i = 0; I <= MAX; i++) {
printf ("%d", a[i]);
}
printf ("\ n");
return 0;
}

Children's shoes can run on your computer above the code, there are surprises Austria

Still waiting for the output? Don't wait, there will never be an output, because this is a dead loop. So what's the death cycle? This will be a talk about the scope of computer integer data and data overflow.

See here, we have to think: for unsigned char i = 255;i + 1 =?

The subtraction in the computer is not the same as in our usual maths, because in mathematics you have assumed that the data range is infinite. However, in the computer, resources are limited, in order to make full use of resources, we specify the data types that occupy different bits. such as unsigned char, which is the same as Char is 8 bits, the difference is not used as the first sign bit, so there are: 0000 0000-1111 1111, binary conversion to decimal is 0-255. The above procedure is now discussed:

1111 1111

+ 1

--------------------

1 0000 0000

As we said above, for various data types, the number of digits represented is fixed, then the first 1 of the results will be discarded because there is no local storage, so you are surprised to find that 255 + 1 = 0

In fact, the above calculation process overflow bit, we found that the weighted value to calculate the overflow bit size is equivalent to: Overflow bit *2^8. For example, 1 of the discard above represents the actual size of 1*2^8 = 256,

By analyzing the behavior that you discard above, you will find that there is a mathematical counterpart to the action, which is the modulo

Well, after the above analysis, what should you find, summarize the calculation of unsigned integers, and so on signed integers

For the overflow problem, ask the following questions for consideration:

1. Conversions between signed numbers and unsigned numbers (conversions between different data types)

2. Unsigned addition, unsigned multiplication

3. Signed addition, symbolic multiplication

The data range of computer and mathematics

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.