Home > other >  51 microcontroller programming data overflow
51 microcontroller programming data overflow

Time:10-03

Today at the time of programming, the function of the program is to make the digital tube display larger number, when I make the temp here for 10000000 will appear normal, but for 1000 * 10000 will show 49 what of, should be to show that a mistake, but when a value is 1000 * 10000 and 10000000, I define the temp data type is unsigned long, after trying, later found temp for 32000 + 767 can show, but 32000 + 768 is not ok, why 32000 + 768 will overflow? Unsigned long the largest scope to 32 times the power of 2, isn't it?

CodePudding user response:

 
Unsigned int aa=1000, b=10000;
Unsigned long DaShu=aa; DaShu *=b;

CodePudding user response:

This is a data type conversion problem,
Although the temp data type definition is unsigned long, but the 1000 and 10000 are unsigned int range, the compiler will handle 1000 * 10000 into unsigned int type of operation, also will appear the phenomenon of overflow, as 1/f, for example, the temp assignment first 1000, and then to multiplication, can solve this problem,
Temp=1000;
Temp=temp * 10000;
  • Related