<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>
Date   : Wed, 26 Dec 2007 19:40:14 -0000
From   : dl.harper@... (David Harper)
Subject: Octal and Binary output

Jonathan Graham Harston wrote:

> I've had the following code to output in binary and octal lying
> around in programs for years and have never been able to get it to
> work 100% correctly. Specifically, very large negative numbers,
> with b31=1 and b30=0, convert incorrectly.
>
> REM Binary padded with zeros
> DEFFNb0(A%,N%):LOCAL A$,B$
> IFA%<0:B$="1":A%=A% AND &7FFFFFFF ELSE B$="0"
> REPEAT A$=STR$(A% AND 1)+A$:A%=A% DIV 2:UNTILA%=0
> =RIGHT$(STRING$(N%,B$)+A$,N%)

Your problem is that if b31=1 and b30=0, then the "REPEAT...UNTIL" is 
satisfied in less than 31 loops. The last line then merely pads it on the 
left with extra "1"'s. The bit you need stick on the left of A$ needs to be 
"100...0" rather than "111...1"

Two possibilities to solve it:

a) Convert the REPEAT loop into a FOR loop so that it always goes round 31 
times (or N%-1 if you prefer); or

b) Replace the last line with something like:
    =B$+STRING$(N%-LENA$-1,"0")+A$

Hope everyone had a good Christmas,

David Harper
<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>