Section 26: Overflow of Multiplication

Publisher:机械梦想家Latest update time:2016-03-10 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Ten years of experience in the industry, teaching you the introductory chapter of microcontrollers, Lecture 26:

The overflow rule for multiplication is the same as the overflow rule for addition. Here is an example:

unsigned char k=30;

unsigned char n=10;

unsigned char a;

a=k*n;

analyze:

The multiplication of k and n is equivalent to 30 multiplied by 10. The result is 300 (0x012c in hexadecimal) which is saved in a hidden intermediate variable. According to the rule of the previous addition operation, I guess that this hidden intermediate variable may be of unsigned int type. Then this intermediate variable is assigned to the single-byte variable a. a can only receive the low 8-bit byte 0x2c in hexadecimal. Therefore, after the operation, the value of a becomes 0x2c in hexadecimal (44 in decimal) due to overflow.

Since the overflow rule of multiplication is the same as that of addition, I will not give more examples. In actual projects, in order to avoid the problem of overflow by mistake, I strongly recommend that all variables involved in addition, subtraction, multiplication and division should be converted into unsigned long variables. The conversion method is the same as the conversion method for addition and subtraction operations, so I will not explain this in detail.

Now write a program to practice what we just talked about, and finally compile the program and download it to the Jianhong 51 learning board to observe the results. Please directly copy the template program in Section 10, and modify the main program code as follows:

void main() //Main program

{

/*---Beginning of C language learning area-----------------------------------------------------------------

----------*/

unsigned char k=30;

unsigned char n=10;

unsigned char a;

a=k*n;

GuiWdData0=a; //Put the value of variable a into window variable 0 for display

/*---End of C language learning area-----------------------------------------------------------------

----------*/

while(1)

{

initial();

key_service();

display_service();

}

}

How to view the calculation results. How to observe variables on the Jianhong 51 learning board? Press the S1 or S5 button to switch to different windows to display different variables. Press the S9 button and hold it to switch to the hexadecimal display interface. After releasing it, it will automatically switch to the decimal interface. The results of observing the program execution on the Jianhong 51 learning board are as follows:

The variable a is 0x2c (44 in decimal).

Preview of the next section: Common formats for division operations.

Reference address:Section 26: Overflow of Multiplication

Previous article:Section 27: Operations of finding quotients by integer division
Next article:Section 25: Abbreviations for continuous multiplication and self-multiplication

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号