uint32_t result of uint16_t addition
-
Hi, im having trouble understanding this line of C code (XC32, pic32):
uint16_t Cur = 65535;
uint16_t Next = 14000;
uint32_t ResVal = 0;
ResVal = ((uint32_t)Cur + (uint32_t)Next);I believe the C standard requires that some sort of integral promotion takes place here but the result is not 79535. Instead i get an overflown uint16_t result. How can i add two uint16_t variables and get the correct uint32_t result?
-
Hi, im having trouble understanding this line of C code (XC32, pic32):
uint16_t Cur = 65535;
uint16_t Next = 14000;
uint32_t ResVal = 0;
ResVal = ((uint32_t)Cur + (uint32_t)Next);I believe the C standard requires that some sort of integral promotion takes place here but the result is not 79535. Instead i get an overflown uint16_t result. How can i add two uint16_t variables and get the correct uint32_t result?
That should work even without casting. For your example an optimising compiler would just assign 79535 to
ResVal
. The XC32 compiler is gcc based. So I don't think that there is a compiler problem. -
Hi, im having trouble understanding this line of C code (XC32, pic32):
uint16_t Cur = 65535;
uint16_t Next = 14000;
uint32_t ResVal = 0;
ResVal = ((uint32_t)Cur + (uint32_t)Next);I believe the C standard requires that some sort of integral promotion takes place here but the result is not 79535. Instead i get an overflown uint16_t result. How can i add two uint16_t variables and get the correct uint32_t result?
In addition to what Jochen said, sometimes looking at the disassembly is helpful.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
-
Hi, im having trouble understanding this line of C code (XC32, pic32):
uint16_t Cur = 65535;
uint16_t Next = 14000;
uint32_t ResVal = 0;
ResVal = ((uint32_t)Cur + (uint32_t)Next);I believe the C standard requires that some sort of integral promotion takes place here but the result is not 79535. Instead i get an overflown uint16_t result. How can i add two uint16_t variables and get the correct uint32_t result?
-
Hi, im having trouble understanding this line of C code (XC32, pic32):
uint16_t Cur = 65535;
uint16_t Next = 14000;
uint32_t ResVal = 0;
ResVal = ((uint32_t)Cur + (uint32_t)Next);I believe the C standard requires that some sort of integral promotion takes place here but the result is not 79535. Instead i get an overflown uint16_t result. How can i add two uint16_t variables and get the correct uint32_t result?
First of all: Casting happens implicitly so you don't need to cast in this scenario. Secondly: These types that you are specifying in non-standard types, so uint32_t could be defined as anything which will give you an incorrect result. Thirdly: How to you check what the result is? Though debugging or though a print statement? The print statement could be wrong. Fouth: Know your platform you writing the code for (its limitations and the standard it is using). Not necessarily that the pic32 uses the specific c standard which you are looking at.
"Program testing can be used to show the presence of bugs, but never to show their absence." << please vote!! >>