Calculate signed and unsigned
2011-09-02 by Антощенков Роман Викторович
Yahoo Groups archive
Index last updated: 2026-04-28 22:41 UTC
Thread
2011-09-02 by Антощенков Роман Викторович
Hello! I using AtMega32A, AVRStudio 4.18SP3 and WinAVR 2011. unsigned int param1; signed int param2; ( unsigned int param3; OCR1B = (unsigned int)(param1 - param2); OCR1A = (unsigned int)(param1 - param2 - param3); I do not get the correct result. How to calculate signed and unsigned variables? -- Best regards, Roman Antoshchenkov Kharkiv Ukraine +38-057-732-97-95 +38-066-605-95-47 ICQ: 461-481-952 mailto:djantoxa@rambler.ru
2011-09-02 by Cat C
I would use uint16_t instead of unsigned int. int16_t instead of int. Cast to (uint16_t) I never know what an int size is :-) Your operations might result in numbers greater than 16 bits; must make sure you deal with that. Cat > To: AVR-Chat@yahoogroups.com > From: djantoxa@rambler.ru > Date: Sat, 3 Sep 2011 01:10:58 +0300 > Subject: [AVR-Chat] Calculate signed and unsigned > > Hello! > > I using AtMega32A, AVRStudio 4.18SP3 and WinAVR 2011. > > unsigned int param1; > signed int param2; ( > unsigned int param3; > > OCR1B = (unsigned int)(param1 - param2); > OCR1A = (unsigned int)(param1 - param2 - param3); > > I do not get the correct result. How to calculate signed and unsigned variables? > > -- > Best regards, > Roman Antoshchenkov > > Kharkiv > Ukraine [Non-text portions of this message have been removed]
2011-09-02 by Cat C
Or in fact if your results may be > 16 bits you can use uint32_t result = param1 - param2 then check if > 65535 and deal with it. etc > To: avr-chat@yahoogroups.com > From: catalin_cluj@hotmail.com > Date: Fri, 2 Sep 2011 16:38:28 -0600 > Subject: RE: [AVR-Chat] Calculate signed and unsigned > > > I would use > uint16_t instead of unsigned int. > int16_t instead of int. > > > Cast to (uint16_t) > > > I never know what an int size is :-) > > > Your operations might result in numbers greater than 16 bits; must make sure you deal with that. > > > Cat > > > To: AVR-Chat@yahoogroups.com > > From: djantoxa@rambler.ru > > Date: Sat, 3 Sep 2011 01:10:58 +0300 > > Subject: [AVR-Chat] Calculate signed and unsigned > > > > Hello! > > > > I using AtMega32A, AVRStudio 4.18SP3 and WinAVR 2011. > > > > unsigned int param1; > > signed int param2; ( > > unsigned int param3; > > > > OCR1B = (unsigned int)(param1 - param2); > > OCR1A = (unsigned int)(param1 - param2 - param3); > > > > I do not get the correct result. How to calculate signed and unsigned variables? > > > > -- > > Best regards, > > Roman Antoshchenkov > > > > Kharkiv > > Ukraine > > > > [Non-text portions of this message have been removed] > > > > ------------------------------------ > > Yahoo! Groups Links > > > [Non-text portions of this message have been removed]
2011-09-02 by David Kelly
On Sep 2, 2011, at 5:10 PM, Антощенков Роман Викторович wrote: > Hello! > > I using AtMega32A, AVRStudio 4.18SP3 and WinAVR 2011. > > unsigned int param1; > signed int param2; ( > unsigned int param3; > > OCR1B = (unsigned int)(param1 - param2); > OCR1A = (unsigned int)(param1 - param2 - param3); > > I do not get the correct result. How to calculate signed and unsigned variables? You get exactly what you asked for. param1 and param3 get promoted to signed because you mixed signed in with the math. The cast only occurs after the math. I don't know what you expect or what values you are using but guessing the following. As others have suggested "int" is an indeterminate size depending on the CPU and compiler options. But uint16_t and int16_t are the same everywhere. Get used to it. OCR1B = (uint16_t)param1 - (uint16_t)param2; OCR1A = (uint16_t)param1 - (uint16_t)param2 - (uint16_t)param3; -- David Kelly N4HHE, dkelly@HiWAAY.net ======================================================================== Whom computers would destroy, they must first drive mad.