Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

16 bit Microcontrollers and integers larger than 16 bit

Status
Not open for further replies.

Dsp121

New Member
I have a beginner's question on 16 bit microcontrollers like the dsPIC30F4011.
If a program in C code using arithmetic with signed long integers produces a result larger than 16 bit, what will happen? Will the 16 bit registers in the micro just overflow or will the number be divided into two 16 bit registers?

I have tried to code some arithmetic like the above but neither the compiler nor the micro gave any errors when building or programming the micro.

Thanks for your help
 
Look up "integer promotion in C" and "type casting in C". The rules will be followed.

Unless you have an operation which implicitly gets promoted to a larger type of variable, or explicitly cast it into a larger type, C will readily allow you to overflow an integer and you'll get the resulting wraparound. The processor has no protection against this.

An overflow would be if the operation's result would not fit into the destination variable. A signed 16-bit int is from -32768 to 32767.

Note that the left-hand side of the equation- the destination- does not dictate the type of the calculation. The right side does. It's possible to overflow the type used on the right even though the destination on the left can contain the result.

Note that it's fully possible to write a complex equation with multiple ops and parenthesis on the right side and get screwed up because ONE stage of the calculation overflows the variable size at some point, even though the ultimate result, on a calculator, is within the -32768 to 32767 range.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top