Peter wrote: > gcc does indeed do sign extended right shift of negative integers, with > horrible results Actually, doing a arithmetic shift on signed numbers and a logical shift on unsigned sounds like the single most reasonable way to implement the C >> operator on machines where arithmetic and logical shifts have the same cost. This is also totally consistant with the C standard, so the results should be neither horrible nor unexpected. > According to gcc -1 >> 1 = -1 which is a little bit sick. No, it is the most reasonable outcome and totally within the standard. If there is anything stupid going on here, it's someone writing C code not expecting this as a possibility. Let's say we're on a machine where these numbers are 16 bit integer for simplicity. Would you rather -1 >> 1 be 32767? I think -1 makes more sense, although both would actually be legal C. > Also -5 >> 1 = -3 (should be -2). You need to go look up bit shifts and the C standard. -5 in binary is ..11011. Shifting it right one bit yields ...11101, which is -3 if a arithmetic right shift was used and 32765 if logical shift was used (again using 16 bit words for simplicity). I'd say -3 is rather more intuitive, but no way should the answer be -2, which is a bit pattern of ...11110 and would also violate the C standard (for good reason). I don't like the way the C >> operator is defined, but in this case (-5 >> 1) being -3 is both correct and the most logical choice. > One would expect the >> to shift > bits, without sign extension, Not if one had bothered to read the description of ">>". > I would expect >> to *always* take the lhs argument as an unsigned > integer and shift it right, and *not* sign extend it. Wrong. RTFM (where M is the C standard). ******************************************************************** Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products (978) 742-9014. Gold level PIC consultants since 2000. -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist