At 10:01 11/18/2001 +0000, Ian Chapman wrote: >Maybe I'm missing something here, but it seems to me that this approach >only works correctly for the cases where |c0-c1| < 128 (perhaps this is >Gerhard's point about overflow not being taken into account). This is correct. This calculation is in the "signed char domain", where values with |value|>128 are not defined and results that fall into this range may not behave as expected. >Nasty, eh? > >Of course, this code would work as intended in a compiler which treats >"chars" as 16-bit values (signed or unsigned). One of the reasons why I >have traditionally disliked 'C' in an embedded environment is that I >have had to put a lot of effort into "working around" assumptions made >by the compiler in order to get sensible code. I don't think a lot of effort is needed. Whenever I start to use a new compiler, I read once through the docs and "translate" the compiler's types into my own types (i8, u8, ...). And the other thing you have to take into account is that C (as low level language) does not handle overflow on integer types. On any C compiler I've seen, an addition like (255 + 1) on an 8bit unsigned type results in 0, not in an overflow. C mostly behaves pretty much as assembler would, too. (Which is the reason why I prefer it in embedded environments... :) ge -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads