[A83] Re: Signed division by a power of two.


[Prev][Next][Index][Thread]

[A83] Re: Signed division by a power of two.



On Tue, 12 Aug 2003 16:01:46 -0500
"Matthew Marshall" <mmarshall@myrealbox.com> wrote:

> When dividing a signed number by two, is there any better way than this?
> 
> bit 7,a
> jp z,Positive
> scf
> rr a
> jp EndPositive
> Positive:
> sra
> EndPositive:
> 
> Or does that way even work?  I have been having a hard time catching onto this idea of signed numbers.
> 
> Thanks,
> MWM
> 

Du you use signed magnitude or 2's complement? (I don't think you use one complement or binary offset...)

If you use 2's complement, just du a simple sra...
sra a

If you use signed magnitude you have to shift the databits, and save the sign bit.

sra a    ;shift databits. sra saves the signbit, but it will be copied to bit 6
res 6,a  ;delete bit 6


Some notes:
In 2's complement, if the number is odd, it will be rounded down... -3/2 will be -2, -1/2 will be -1, and 3/2 will be 1.

In signed magnitude, the number will be rounded toward zero... -3/2 -> -1 and 3/2 -> 1... 


if you have 16-bit numbers:

2's complement:

sra h
rr l

signed magnitude:

sra h
rr l
res 6, h



NO CODE IS TESTED, but it should work!!!

-- 
Max Sikstrom
pengi@molik.org




References: