Data Types

Hello. Struggling with getting my head around data types. Clearly a newcomers question, but why does this produce 4.2*10^9?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// playing with variables

#include <iostream>
using namespace std;

int main()
{
    int a(2);
    unsigned int b(3);
    float result;
    result=a-b;
    cout<<result;
    return 0;
}


Thanks. Feel free to insult the sheer ignorance.
Last edited on
4294967295 is 1111 1111 1111 1111 1111 1111 1111 1111 in binary.
Hmm, isn't that interesting?

Actually, I don't know the answer to your question. I just wanted to point that out. I think it's significant somehow.
Last edited on
No insults for ignorance needed. magicalblender is correct that the binary representation has some significance. The easy answer is that when you have an operation between a signed int and an usigned int, the int value is converted to an unsigned int. (It's one of those 'implicit conversions' you'll need to watch out for.) Since the result of your subtraction is a negative value, how should the computer handle it? Well, the answer to that is a little harder to understand. Essentially the computer is returning the 'two's complement' of the original unsigned int. Try multiplying b by a negative one and you'll see that it produces the same result. I don't really understand very much about it, other than to say it's a method computers can use to represent negative numbers. I also don't know why it was implemented this way. It would make more sense to me if the compiler just returned an error saying that it's an illegal action. If you want to read more information on two's complement, wikipedia has an article on it here:

http://en.wikipedia.org/wiki/Two's_complement

The best advice I can give is to be very careful about when you use unsigned int. If you use them in applications where they may be part of a function that produces a negative result, you'll end up with results that are very hard to predict.

Hope this helps.
Last edited on
thats brilliant. thanks very much.
Topic archived. No new replies allowed.