Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

-2 < 1 = false. Why?

Hy! Sorry for my bad english, anyway the questions is:

I have this code in objective-c:

unsigned int a = 1; 
int b = -2
if (b < a);

I expect true and instead the result of the if(b < a)is false, why?

like image 386
Antonio Barra Avatar asked Nov 29 '22 17:11

Antonio Barra


2 Answers

C automatically converts -2 into an unsigned int in the comparison. The result is that the comparison is actually (4294967294 < 1), which it is not.

like image 126
EmeryBerger Avatar answered Dec 10 '22 10:12

EmeryBerger


You are comparing signed to unsigned. The signed value is promoted to unsigned, which results in a large number (0xFFFFFFFD I think) which is definitely bigger than 1

like image 29
SRM Avatar answered Dec 10 '22 09:12

SRM