4

As the title suggests, I am curious about how an unsigned int (or things like NSUInteger, u_int_blah, but I assume these are all typedefs of the same thing) works. For example, when their value drops below zero, is an exeption raised? Will an error occur? One specific example of this would be indirectly setting the value to a negative number.

for (unsigned int x = 5; x > -10; x--) {
    // will x ever reach below zero, or will the loop terminate
}


Also, another way to indirectly set it would be to have the user input it.

printf("Enter a number");
unsigned int x;
scanf("%ud", &x); // user enters something like -29


So really, I have three questions. What stops and unsigned int from being assigned to a negative number (unsigned int x = - 3). How is this behavior implemented (by the compiler, or by other means). What happens when an unsigned int is assigned (directly or indirectly) to a negative value. Is the data corrupted? Does it overflow?
Thankyou

Brian Tracy
  • 6,801
  • 2
  • 33
  • 48
  • possible duplicate of [Is unsigned integer subtraction defined behavior?](http://stackoverflow.com/questions/7221409/is-unsigned-integer-subtraction-defined-behavior) – Qantas 94 Heavy Apr 02 '14 at 02:51
  • That question is asking about what happens when unsigned ints are subtracted and added. I am wondering about how they are enforced by the compiler and what happens when they drop below zero. – Brian Tracy Apr 02 '14 at 02:53
  • Is it not asking about "subtracting an unsigned integer" when "the result would be negative"? – Qantas 94 Heavy Apr 02 '14 at 02:54
  • I see the similarities, but there are other behavioral questions addressed in mine. – Brian Tracy Apr 02 '14 at 02:55
  • You have a few different question here, for the case of assigning a signed number to an unsigned variable my [answer here](https://stackoverflow.com/questions/22131388/is-conversion-int-unsigned-long-long-defined-by-the-standard/22131521#22131521) should be helpful. If I get some more time I will back on this. – Shafik Yaghmour Apr 02 '14 at 03:04

3 Answers3

3

When a unsigned comparing with signed, they will all be cast into unsigned. The procedure is relative to how the data is stored in memory. In binary, a minus number( like -3), will be stored like :

-3 : 1111 1111 1111 1101
3  : 0000 0000 0000 0011

you can tell that -3 can be like :

// result  : 0000 0000 0000 0011

result = for_every_bit_of_3( not **ThisBit** );  
// result  : 1111 1111 1111 1100 

result = result + 1;
// result  : 1111 1111 1111 1101 

So the loop:

for (unsigned int x = 5; x > -10; x--) {
    // will x ever reach below zero, or will the loop terminate
}

will be like

// 4,294,967,286 is what -10 cast to unsigned
for (unsigned int x = 5; x > 4294967286; x--) {
    // will x ever reach below zero, or will the loop terminate
}
Jim Yang
  • 479
  • 2
  • 12
2

When you assign a negative number

unsigned int = -1;

The number you got will be 4294967295, or 4^8 -1

because the size of integer is 4bytes, or 4^8 = 4294967296

The integer will wrap around this number if it is negative

John
  • 76
  • 6
  • So you can imagine if the counter of the loop goes into a negative number, the value will become a very big as 4294967295...4294967294...4294967293...etc. It won't crash the program but you will hardly get correct results you want. – John Apr 02 '14 at 10:33
1

In answer to your first code example, the loop will compile (though will raise a warning if you are compiling with gcc and have the -Wno-sign-compare flag). Running it, however, will generally result in the loop not running at all since most systems use Two's complement (http://en.wikipedia.org/wiki/Two%27s_complement) which means -10 is the same as 4294967286 (assuming 4 byte integers but in general 2*INT_MAX - 10) which is greater than 5. In general, I recommend reading up on Two's complement as it will probably answer all your questions on the matter.

Kevin
  • 247
  • 1
  • 6