All, Would it make sense that... (1) if the signed and unsigned integers that you are referring to are 16 bits lets say. (2) 500 decimal would be represented in 16 bits (Unsigned) as 0000 0001 1111 0100 (0x01F4). (3) -100 decimal would be represented in 16 bits ( Signed ) as 1111 1111 1001 1100 (0xFF9C). 100d =3D 1100100b. as 16 bits it would be 0000000001100100. And taking the twos compement=20 would give 1111111110011100 shown above. =20 If the above statements are true, then processing a signed number (Line 3) as an unsigned number=20 (Like Line 2) would give the result that -100 would be larger than 500. Is this not true?=20 =20 Regards, Jim > -------- Original Message -------- > Subject: RE: [OT] Why on earth??!? > From: "Bob Ammerman" > Date: Fri, May 02, 2014 10:29 am > To: "'Microcontroller discussion list - Public.'" >=20 >=20 > Yes, many compilers will give you a warning about comparing signed to > unsigned. You really just shouldn't do it! >=20 > If you think about what happens at the hardware level, where the comparis= on > is basically a subtraction, you can't properly interpret the flags (C an= d Z > on a PIC) set by the comparison. >=20 > -- Bob Ammerman > RAm Systems >=20 >=20 > -----Original Message----- > From: piclist-bounces@mit.edu [mailto:piclist-bounces@mit.edu] On Behalf = Of > Electron > Sent: Friday, May 02, 2014 10:43 AM > To: Microcontroller discussion list - Public.; Microcontroller discussion > list - Public. > Subject: [OT] Why on earth??!? >=20 >=20 > Hello! >=20 > a is a signed int and contains -100 > b is an unsigned int and contains 500 >=20 > a=20 > Why on earth? :P >=20 > I can cast b to signed int and get the correct results, but leaving as is > instead the result surprises me a lot, as I have no idea why -100<500 sho= uld > be false, it's like if the compiler casts a to an unsigned type > automatically (and this was clearly not requested by the programmer). >=20 > If we keep them as they are, i.e. the first signed and the second unsigne= d, > then why should a=20 > This is very confusing really. >=20 > Now I have to correct all of my code, looking for comparisons between sig= ned > and unsigned ints, and cast both variables to the type I mean. :-/ >=20 > Is there any other situation I have to be careful about when mixing signe= d > and unsigned types? >=20 > Please do not reply the obvious "generically the use of unsigned types is > not adviceable, why don't you stick with only signed types? you will be m= uch > safer". THANKS. >=20 > Cheers, > Mario >=20 > -- > http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/cha= nge > your membership options at http://mailman.mit.edu/mailman/listinfo/piclis= t >=20 > --=20 > http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .