Converting Unsigned to Signed

Objects of unsigned integral types can be converted to corresponding signed types. However, such a conversion can cause misinterpretation of data if the value of the unsigned object is outside the range representable by the signed type, as demonstrated in the following example:

Example

// conve__pluslang_Converting_Unsigned_to_Signed.cpp
// compile with: /EHsc
#include <iostream>

using namespace std;
int main()
{
 short  i;
 unsigned short u = 65533;

 cout << (i = u) << "\n";
}

Output

-3

In the preceding example, u is an unsigned short integral object that must be converted to a signed quantity to evaluate the expression (i = u). Because its value cannot be properly represented in a signed short, the data is misinterpreted as shown.

See Also

Reference

Integral Conversions