Use block IFs instead of trying to cram it all onto one line. You might also want to come up with a more intuitive method of representing your code too, as that looks rather inefficient. Decimal-to-binary converter, eh?
I can make two suggestions right off the bat:
1. b0 b1 b2 etc. could be redone as an array.
2. Building off of #1, you could simply use a loop to set them all to 0 at the beginning of your loop. That would eliminate the need for the ELSE clause, since they would already all be 0 unless explicitly set otherwise. Or, if you don't want to change your variables as per #1, you could just have b0 = 0: b1 = 0: etc. at the beginning of your loop.
Nodtveidt wrote:Use block IFs instead of trying to cram it all onto one line. You might also want to come up with a more intuitive method of representing your code too, as that looks rather inefficient. Decimal-to-binary converter, eh?
Yes . I've found that the problem is the binary-to-number converter, because it has so many dimensions (8!). So I've found that I can solve this by using 8 arrays: a Mul1, a Mul2, a Mul4, 8, 16, 32, 64 and 128. Then I can add the results together.
So thanks for your help, but I've found a solution.
BTW: It really doesn't matter if this code is inefficient (but it is indeed ), because it are all precalculations.
Codemss wrote:It really doesn't matter if this code is inefficient because it are all precalculations.
Well, I sure would like to see your final application that required the BtN-array. I agree that pre-computing the NtB-array is useful because it is easy to use, but I would guess the other is as hard to use as just computing. Below is how I would handle NtB and BtN.
DECLARE FUNCTION BtN% (Binary$)
DIM NtB(255) AS STRING: GOSUB InitNtB
FOR i = 0 TO 255
Test$ = NtB(i)
Test% = BtN(Test$)
IF Test% <> i THEN STOP: 'bug in program
NEXT i
PRINT "Works"
SYSTEM
InitNtB:
FOR n = 0 TO 255
IF (n AND 128) THEN w$ = "1" ELSE w$ = "0"
IF (n AND 64) THEN w$ = w$ + "1" ELSE w$ = w$ + "0"
IF (n AND 32) THEN w$ = w$ + "1" ELSE w$ = w$ + "0"
IF (n AND 16) THEN w$ = w$ + "1" ELSE w$ = w$ + "0"
IF (n AND 8) THEN w$ = w$ + "1" ELSE w$ = w$ + "0"
IF (n AND 4) THEN w$ = w$ + "1" ELSE w$ = w$ + "0"
IF (n AND 2) THEN w$ = w$ + "1" ELSE w$ = w$ + "0"
IF (n AND 1) THEN w$ = w$ + "1" ELSE w$ = w$ + "0"
NtB(n) = w$
NEXT n
RETURN
FUNCTION BtN% (Binary$)
IF LEN(Binary$) <> 8 THEN STOP: 'Misuse of this function
IF MID$(Binary$, 8, 1) = "1" THEN w = 1 ELSE w = 0
IF MID$(Binary$, 7, 1) = "1" THEN w = w + 2
IF MID$(Binary$, 6, 1) = "1" THEN w = w + 4
IF MID$(Binary$, 5, 1) = "1" THEN w = w + 8
IF MID$(Binary$, 4, 1) = "1" THEN w = w + 16
IF MID$(Binary$, 3, 1) = "1" THEN w = w + 32
IF MID$(Binary$, 2, 1) = "1" THEN w = w + 64
IF MID$(Binary$, 1, 1) = "1" THEN w = w + 128
BtN% = w
END FUNCTION