Menu

#61 right shift operator (>>) is logical, not arithmetic

closed-rejected
gcc (183)
5
2010-11-06
2010-11-05
No

Right shift operator is logical, i.e. it does not extend the sign bit.
So the operation on negative numbers give wrong results.

Discussion

  • Peter A. Bigot

    Peter A. Bigot - 2010-11-06

    To clarify: the right shift operator SHOULD be logical, but in mspgcc an arithmetic shift is used. This is probably because the MSP430 has no instruction for logical shifts (it was added in MSP430X). Logical right shift must be emulated by clearing the carry bit and using rotate-through-carry.

    This should be fun to fix....

     
  • Peter A. Bigot

    Peter A. Bigot - 2010-11-06

    On further investigation: in C a right shift of a negative signed integer is implementation defined. GCC uses arithmetic shift in this case, but logical shift for an unsigned integer. I have verified that the behavior of code compiled by mspgcc is the same as stock gcc 4.4.3 on an i686 platform.

    Non-discrepant; no change will be implemented.

     
  • Peter A. Bigot

    Peter A. Bigot - 2010-11-06
    • status: open --> closed-rejected
     
  • Jerry Mannil

    Jerry Mannil - 2010-11-07

    Thanks Peter for the clarification. Is this the behavior for 32 bit signed numbers too on MSP430 ?. It was a while ago, but i remember facing the issue with 32 bit signed number on MSP430 on Telosb motes. The right shift was not giving me the correct results, and i had to use the divide operator.

     
  • Peter A. Bigot

    Peter A. Bigot - 2010-11-07

    This is the behaviour for 16, 32, and 64-bit integers, according to my testing.

     

Log in to post a comment.

MongoDB Logo MongoDB