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.
Previous article:Section 27: Operations of finding quotients by integer division
Next article:Section 25: Abbreviations for continuous multiplication and self-multiplication
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Ask a question about the make command
- What is the resistance value of a 0Ω resistor? How much current can a 0Ω resistor pass?
- APWM
- Industrial computer case
- EEWORLD University Hall----Live Replay: Datang NXP-DNS Battery Management Chip Solution
- 5G Network Slicing
- Static Electricity Protection Measures in Circuits
- Portable energy storage power supply-Nanxin Semiconductor enters the market with strength
- Summary of the characteristics of 5 wireless transmission protocols for the Internet of Things
- Can dual 12V power supplies be used in parallel?