It basically behaves like this (using a 3 bit number here):
Code:
binary unsigned signed
000 0 0
001 1 1
010 2 2
011 3 3
100 4 -4
101 5 -3
110 6 -2
111 7 -1
Demo:
Code:
void print(unsigned short us) {
cout << "unsigned(" << us << ") -> signed(" << static_cast<signed short>(us) << ")\n";
}
int main()
{
unsigned short us;
// Lower values identical
for (us = 0; us < 8; ++us) {
print(us);
}
cout << '\n';
// Mid values, signed type becomes negative (starting with maximum negative value)
for (us = 32764; us < 32772; ++us) {
print(us);
}
cout << '\n';
// Highest values, signed type has counted down to -1
for (us = 65528; us < 65535; ++us) {
print(us + 1);
}
cout << '\n';
}