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?
I'm trying to understand the problem when it comes to mixing signed and unsigned ints.
a < b
By the usual arithmetic conversions b is converted to an unsigned int, which is a huge number > a.
Here the expression a < b is the same as:
2U < (unsigned int) -2 which the same as:
2U < UINT_MAX - 1 (in most two's complement systems) which is 1 (true).
With two's compliment there is no bit reserved for the sign like the "Sign-and-magnitude method".
In two's complement representation if the most significant bit of a signed quantity is 1, the number is negative.
What would be the representation of 2 147 483 648 be?
10000000 00000000 00000000 00000000
What would be the representation of -2 147 483 648 be?
10000000 00000000 00000000 00000000
The same! Hence, you need a convention to know the difference. The convention is that the first bit is still used to decide the sign, just not using the naïve sign-magnitude method you would otherwise use. This means every positive number starts with 0, leaving only 31 bits for the actual number. This gives half the positive range of unsigned numbers.
This problem with your code is that the signed integer will be converted to unsigned. For example, -1 will become 4 294 967 295 (they have the same binary representation), and will be much larger than zero, instead of smaller. This is probably not what you expect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With