Signed/Unsigned integer division

do the SHL and SHR (shift) commands have the same effect on signed numbers as unsigned numbers - i read somewhere that there were some specific shift commands for multiplying/dividing by 2 on signed numbers. i really DON'T want to use imul/idiv for speeds sake.


cheers





Comments

  • : do the SHL and SHR (shift) commands have the same effect on signed numbers as unsigned numbers - i read somewhere that there were some specific shift commands for multiplying/dividing by 2 on signed numbers. i really DON'T want to use imul/idiv for speeds sake.


    Nope. There is a shift that's meant for signed, and one that's meant for unsigned. SHR will shift and than add 0's to the high bit(s), no matter what. If you want to shift signed numbers, and preserve the sign, use SAR/SAL. They shift and sign extend, so that shifting a negative number to the right adds a 1 bit to the high bit(s).


    Rock


  • : : do the SHL and SHR (shift) commands have the same effect on signed numbers as unsigned numbers - i read somewhere that there were some specific shift commands for multiplying/dividing by 2 on signed numbers. i really DON'T want to use imul/idiv for speeds sake.


    : Nope. There is a shift that's meant for signed, and one that's meant for unsigned. SHR will shift and than add 0's to the high bit(s), no matter what. If you want to shift signed numbers, and preserve the sign, use SAR/SAL. They shift and sign extend, so that shifting a negative number to the right adds a 1 bit to the high bit(s).


    : Rock


    That's not entirely true. Only SAR preserves the

    MSD and can therefore be used for signed division.

    SAL is identical to SHL and they produce the same

    machine code.





Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories