3

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?

user974967
  • 2,928
  • 10
  • 28
  • 45

5 Answers5

6

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.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • This is the right answer (although it uses C instead of C++ terminology). Who downvoted this? Explanation? – Konrad Rudolph Jun 03 '12 at 15:09
  • @KonradRudolph originally, this answer was only "By the usual arithmetic conversions b is converted to an unsigned int, which is a huge number > a." – Luchian Grigore Jun 03 '12 at 15:11
  • @LuchianGrigore which was correct and I expanded my answer. Actually all the answers were downvoted, so I think this is just the action of some angry guy. – ouah Jun 03 '12 at 15:15
  • I'm not sure where my confusion is setting in. I think it's in trying to understand why casting the int -2 to an unsigned int results in a huge number. If you have an int -2 and an unsigned int -2, aren't they represented the same in memory (like I have above)? – user974967 Jun 03 '12 at 15:28
1

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.

FrederikVds
  • 551
  • 4
  • 11
  • If you have the int -2 and the unsigned int -2, aren't they still represented the same in memory? Why does casting the int -2 to an unsigned int result in that huge number? Are you saying that signed and unsigned numbers are represented the same in memory, just interpreted differently? – user974967 Jun 03 '12 at 15:48
  • @user974967: Yes, interpretation is the key; 11111111 interpreted as a `signed char` means -1, but interpreted as an `unsigned char` it means 255. – molbdnilo Jun 03 '12 at 16:52
0

An int can only store 2^32 different values (if it's 32bit), whether it is signed or unsigned. So a signed int has 1/2 of that range below zero, and 1/2 of that range above zero. An unsigned int has that full range above zero.

While they don't call the most significant bit of a signed int a 'sign bit', it can be treated that way.

huysentruitw
  • 27,376
  • 9
  • 90
  • 133
0

Isnt this as easy as (int)a < int(b)??? Isnt C++ like you need to strongly do explicit type casting?

Andrew
  • 1,037
  • 9
  • 17
-2

Well, the -1 as signed int is -1 and as unsigned int is 65534, so the problem is with signed values where "-" is required. In case of returning -1 error code, with unsigned int that would be 65534 code.

Andrew
  • 1,037
  • 9
  • 17
  • First, this doesn’t answer OP’s question because it doesn’t explain about the problems, it just says “it’s a problem because it’s a problem”. And secondly, it’s wrong (that is, the values are wrong). – Konrad Rudolph Jun 03 '12 at 15:06
  • Well what about casting unsigned ints to ints? Would that mean simply not using "unsigned" any more? – Andrew Jun 04 '12 at 18:08