0

First of all I've been searching for the outcome of a negative input in an unsigned int but can't find out how it is converted. Like for example -3. How does -3 look like and how did it turn out like that?

How about subtracting from it?

What will happen?

#include <stdio.h> 

int main(){ 
unsigned int num1 = 3, num2 = -2; 

printf("num1's initial value is %u.\n",num1); 
printf("num2's initial value is %u.\n",num2); 

num1 = num1 + 1; 
num2 = num2 - 1; 

printf("num1's value is now %u.\n",num1); 
printf("num2's value is now %u.\n",num2); 

num1 = 2147483647; 
num2 = -2147483648; 

printf("num1's value is now %u.\n",num1); 
printf("num2's value is now %u.\n",num2); 

num1 = num1 + 1; 
num2 = num2 - 1; 

printf("num1's value is now %u.\n",num1); 
printf("num2's value is now %u.\n",num2);

return 0;
}
zhouheidz
  • 47
  • 1
  • 1
  • 6
  • It seems you have the code... what happened ? – Laurent S. Oct 28 '14 at 13:07
  • @AdrianoRepetti that post is only about assigning the negative value to the unsigned int. Here, I want to know about what happens when you subtract or add to it. I can't find answers anywhere even in Google. – zhouheidz Oct 28 '14 at 13:10
  • 1
    You first assign a negative value (or you read it), result after an arithmetic operation: http://stackoverflow.com/questions/16056758/c-c-unsigned-integer-overflow (it doesn't matter if it was negative or not, it overflows as usual). – Adriano Repetti Oct 28 '14 at 13:11
  • 1
    @dasblinkenlight The negative value to assign is converted to the unsigned type as if by repeated addition or subtraction of `TYPE_MAX + 1`, until it is in range. Two's complement does not come into it. – Pascal Cuoq Oct 28 '14 at 13:25

2 Answers2

0

Assigning a negative number is the same as assigning 0 and subtracting that number. When this subtraction occurs, your number overflows and becomes huge;

If x is the bigger number you can store, x + 1 = 0 (overflows and goes around in a loop), which means that when you perform 0 - 1 in an unsigned number, you're actually getting the biggest number you can store.

Only when you subtract beyond 0 or sum beyond the max number the things behave differently. After that those numbers will behave like any others.

SlySherZ
  • 1,631
  • 1
  • 16
  • 25
0

Okay, Let's go line by line..

1 . unsigned int num1 = 3, num2 = -2;

for num2 = -2 ; It will show as a positive integer of value of max unsigned integer - 1 (value depends on computer architecture and compiler).

like if you assign num2 = X (say) and the stored value will be MAX_UNSIGNED_VALUE - (X-1).

Basically It will assign the bit pattern representing -X (in 2's complement) to the unsigned int. Which will be a large unsigned value. For 32 bit ints this will be 2^32 - (X+1).

Next the line

num2 = -2147483648;

So I'm considering 32 bit int it will store the (max value of an unsigned int - 2147483647). that is

num2 = (4294967295-2147483647) = 2147483648

Very detailed explanation about how this conversion happens is given, in the following articles.

Hope this helps!

Community
  • 1
  • 1
Blackhat002
  • 598
  • 4
  • 12