The following program gives a signed/unsigned mismatch warning:
#include <iostream>
int main()
{
unsigned int a = 2;
int b = -2;
if(a < b)
std::cout << "a is less than b!";
return 0;
}
I'm trying to understand the problem when it comes to mixing signed and unsigned ints. From what I have been told, an int is typically stored in memory using two's complement.
So, let's say I have the number 2. Based on what I understand it will be represented in memory like this:
00000000 00000000 00000000 00000010
And -2 will be represented as the one's compliment plus 1, or:
11111111 11111111 11111111 11111110
With two's compliment there is no bit reserved for the sign like the "Sign-and-magnitude method". If there is no sign bit, why are unsigned ints capable of storing larger positive numbers? What is an example of a problem which could occur when mixing signed/unsigned ints?