how to compare unsigned int and int ?

hi


int main()
{
unsigned int u = 10;
int i = -1;


if ( i <= u )
{
cout<<" I is less than "<<endl;
}
else
{
cout<<" U is less than "<<endl;
}

}

the output i'm getting is U is less than. How it works?
Not able to get the internall logic in this. Can any one explain me hw?

Thanks in advance..
it appears that "i" is being cast to an unsigned int which would make it a very large number (the int max)
the way the computer handles negatives is that the number is INTMAX - value ..if that is converted to a unsigned the negative number is suddenly positive .. more technically the highest bit is called the sign bit..it's 1 for negative numbers and 0 for positive..when you relate that to pure counting it means the upper half of the range of an int is considered negative when it's a signed int
If you want to actually compare them, you will have to cast them both up to a higher signed time like a long int.
Topic archived. No new replies allowed.