Bits are bits. An implicit conversion is occuring. Any unsigned int can be represented as a signed int so long as they are the same size. I was confused when I first saw this too (it's not very common for many types of programming) and may differ from compiler to compiler so test it before relying on it. Here's some code that demonstrates how it works using chars to make it simple to understand:
#include <iostream>
int main()
{
unsigned int u = 0;
while ( u != 256 )
{
const signed char s = u; // putting an unsigned type (u) into a signed type (s)
// cast s up here so we don't print the extended ascii char.
std::cout << (signed int)s << " signed == " << u << " unsigned" << std::endl;
++u;
}
return 0;
}
Here's a bit of output from that code:
-128 signed == 128 unsigned
-127 signed == 129 unsigned
-126 signed == 130 unsigned
...
-3 signed == 253 unsigned
-2 signed == 254 unsigned
-1 signed == 255 unsigned
C++11 compilers will generate a narrowing error if you initialize this way:
unsigned int j = {-32768}; // Notice the brackets