sergio masci wrote: >>> byte_var = byte_var + word_var // see (2) >>> (2) ... add operation performed as two bytes >> >> This is surprising, and may lead to a wrong result (for signed variables). >> IMO this should at least trigger a compiler warning. I'd like to get your feedback on this. Is there something I was missing? Assuming all three being signed, at first I thought that something like (-127 + 254) would give a wrong result, but it doesn't: (0x81 + 0xfe) = 0x7f = 127. Does this work for all cases where the result fits in a signed byte? Seems so... and is a bit counterintuitive for me. > I got carried away in my enthusiasm and forgot about the promotion to > "int" in the conditional in C. But what I wrote also holds true for ints > and longs in XCSB whereas it does not in C Yes, I thought about this, but since I didn't know whether you wanted to go there, I didn't include that in my reply. > if unsigned_int_var > signed_int_var then > > always true if unsigned_int_var > 32767 > always false if signed_int_var < 0 > otherwise true or false as expected > endif I think C treats the signed variable as unsigned, without changing its bits, so to speak. Which of course is not good, and may give an unexpected result. I tend to avoid that, unless I can guarantee the sign of the signed variable :) (And it definitely should trigger a warning. I'll have to try to see whether it does.) Your solution definitely makes more sense. > and in anycase although the byte version has the same net effect under > both C and XCSB, the XCSB version does not need to generate zero and sign > extended ints prior to the comparison :-P Since I have never worked with your compiler, I can't really say much. (I'd like to -- you have some interesting ideas --, but you know how that is... you have something that works, so you go with it :) But at least the HiTech PICC compilers seem to do a good job of optimizing those operations, so that unnecessary promotions to int don't get executed. It's also sometimes effective (code-wise) to give the compiler hints in the form of type casts about the expected size of intermediate results. Gerhard -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist