unsigned and signed values

I want to understand how the compiler gives me garbage value when I declare an unsigned int and makes it equal to -2.

I know that unsigned int cannot be negative, hence, giving me garbage value. BUT, I want to know how that works. What happens to the memory address when we process this code? Is test1 not saved anywhere in memory? Help me understand it please.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
	unsigned int test1;
	test1 = -2;

	cout << test1 << endl;

	system("PAUSE");
	return 0;
}


output:
1
2
4294967294
Press any key to continue . . .


Also, I have another question.

When we want to add two values, let's say that we want to add
unsigned u = 10 and int i = -42
usually, the compiler will try to convert one type to another. How does it decide from what type to what type to convert?
In other words, in this case, the int will be converted to unsigned, but why not the opposite?
On what basis does the compiler converts types?
Last edited on
The compiler just gives the garbage value at the variable test1. You cannot predict it. When you try to insert a signed number in an unsigned variable then either the compiler skips that line of code or it just makes the number unsigned. In your case when the compiler sees that a signed number is to be inserted in an unsigned variable it skips it. That is why when you cout test1 you get a garbage value that was stored at the variable location.
@Stalker:
Thats a good question because I've compiled that and i got the same results which would mean that it is something that is IDE independent something specific to the language. It cannot be garbage
closed account (E0p9LyTq)
I want to understand how the compiler gives me garbage value when I declare an unsigned int and makes it equal to -2.


That is not a "garbage" value, it is the unsigned representation of -2. An unsigned integer will never be a negative value, the smallest value it can be is 0 (zero).

The values you get are not garbage. Assigning -1 to an unsigned type will give you the maximum value that variable can hold. Assigning -2 to an unsigned type will give the maximum value that variable can hold minus 1.

1
2
3
4
5
6
7
8
9
10
  #include <iostream>
  #include <limits>

int main()
{
    unsigned num = 0;
    std::cout << "Max value " << std::numeric_limits<unsigned>::max() << std::endl;
    std::cout << "Also max value " << num - 1 << std::endl;
    std::cout << "Max value -1 " << num -2 << std::endl;
}



Output
Max value 4294967295
Also max value 4294967295
Max value -1 4294967294
i did find some information which makes sense. it relates to finding the 2's compliment of a negative number.
Since it was declared as an unsigned int its supposed to store a 1 byte(32 bit) number. When the compiler encounters this negative number it finds the 2's compliment of that 32 bit negative number

Dont take my word for it though, this just makes sense to me

source:
http://stackoverflow.com/questions/2711522/what-happens-if-i-assign-a-negative-value-to-an-unsigned-variable


In relation to the second question what i remember from my microprocessor class:
Remember that the processor only processes information in base 2 ie 0's and 1's. that means that negative numbers essentially doesn't exist to the processor,therefore all negative numbers has to be converted to a positive type first. This is done by using the 2's compliment number system. The negative number will always be converted and not the positive.
Last edited on
Thank you so much. This helped me understand it.
See this converter:
https://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html
In the decimal box, put the value 4294967294 (press return if necessary).

Now, from the drop-down list choose "signed 32-bit"
Now type -2 into the decimal box.

Compare the binary and/or hex values in each case
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
   long aLong = -1;
   
   std::cout << (signed long) aLong << " " << (unsigned long) aLong << "\n";
}
Topic archived. No new replies allowed.