Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
Aamir Ali
02/15/13 09:37
Read: 1327 times


 
#189374 - Using unsigned with signed int
I found this :


If an operation involves both signed and unsigned integers, the situation is a bit more complicated. If the unsigned operand is smaller (perhaps we're operating on unsigned int and long int), such that the larger, signed type could represent all values of the smaller, unsigned type, then the unsigned value is converted to the larger, signed type, and the result has the larger, signed type. Otherwise (that is, if the signed type can not represent all values of the unsigned type), both values are converted to a common unsigned type, and the result has that unsigned type.

Few question:

1. It says "if the signed type can not represent all values of the unsigned type), both values are converted to a common unsigned type, and the result has that unsigned type".

So does that mean before result is used, signed in converted to unsigned & then added to form result?

2. I have one problem where I have two same width vars: uint16_t & int16_t

so what I did:

uint16_t temp;
int16_t temp2;

//.........

if(temp2 > 0)
{
temp = temp + (uint16_t)temp2;
}

else
{
temp2 = -temp2;
temp = temp - (uint16_t)temp2;
}

3.Also how to type cast from int to uint. Is it right method to do

Like :

int16_t temp2;
uint16_t temp;


if(temp2 < 0) // for safe tytpe casting, will limit -ve range, +ve range is auto
temp2 = 0;

temp = (uint16_t) (temp2);
http://www.keil.com/forum/22265/

List of 2 messages in thread
TopicAuthorDate
Using unsigned with signed int      Aamir ALi      02/15/13 09:37      
   good questions, but      Erik Malund      02/15/13 18:07      

Back to Subject List