Vitaliy maksimov.org> writes: > I wasn't following the conversation very closely, can you explain what > you're talking about? What does this have to do with PICduino? gcc does indeed do sign extended right shift of negative integers, with horrible results as Peter Green suggested. picduino uses gcc afaik and P. G. implemented another type of >> for pic32 (?)which behaves differently from the gcc normal broken >>. According to gcc -1 >> 1 = -1 which is a little bit sick. gcc uses the sar instruction to do the shifting (I looked at the assembly output). Also -5 >> 1 = -3 (should be -2). One would expect the >> to shift bits, without sign extension, and in any case without getting stuck on 0xFFFFFFFF (== -1) and I did in fact rely on txd_bit = acc & 1; acc = acc >> 1; many a time (but with a bit counter). Apparently this is not such a good idea when not using a bit counter (using a stop bit in the register and waiting for it to be shifted out). To correctly divide by two a negative number using >> it should sign extended right shift, then add 1 (because it's 2's complement iff the msb is 1 (sign)), but this breaks the desired meaning of >> which is to bit shift to the right. gcc does not do that (gcc for x86), it is broken imho. I would expect >> to *always* take the lhs argument as an unsigned integer and shift it right, and *not* sign extend it. In fact the optimizer should deal with x / 2^N and turn it into shr ax, cl; followed by inc ax; iff ax.31 == 1, which is the correct way to do x /= 2^N;, and programmers should never use >> N to divide by 2^N. Imho programmers should never rely on >> for division of negative numbers, and >> should not do sign extension (it is presumed to do shifting only). E.g. think of >> used to shift a bit pattern to make, say, a running light row or control the phases of a stepper. It is interesting that C does not have a circular bit buffer rotation operator, like rol or ror, which could be implemented using >>> or <<< to encode it f.ex. maybe the machines on which D&R implemented C did not have a rotation instruction. Peter -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist