Number Base Conversion

This file is part of Timex Sinclair Public Domain Library Tape 1001 . Download the collection to get this file.
Date: 198x
Type: Program
Platform(s): TS 1000

This program performs bidirectional number base conversion between binary (base 2) and decimal (base 10). The user first selects a direction: decimal-to-binary conversion uses a successive subtraction algorithm based on powers of 2, iterating from the highest relevant bit down to bit 0 using a logarithm-derived upper bound (LN T / LN 256 * 8). Binary-to-decimal conversion reads a binary string character by character, using VAL on individual characters and accumulating weighted powers of 2. The program loops continuously in either mode until interrupted, with each conversion direction implemented as a separate subroutine-like section starting at lines 110 and 600 respectively.


Program Analysis

Program Structure

The program is divided into two main conversion loops, selected by the initial input at line 70:

  1. Lines 10–230: Initialisation, mode selection, and decimal-to-binary conversion loop.
  2. Lines 600–690: Binary-to-decimal conversion loop.
  3. Lines 700–800: SAVE with auto-run flag, then RUN.

Both conversion sections loop indefinitely using GOTO 110 and GOTO 600 respectively, requiring a manual break to exit. The REM at line 5 displays the program title in inverse video.

Decimal-to-Binary Conversion (Lines 110–230)

The algorithm works by iterating over bit positions from high to low. The upper bound for the loop is calculated at line 160:

FOR J=INT (LN T/LN 256*8) TO 0 STEP -1

This computes ⌊log₂(T)⌋ via the change-of-base formula: LN T / LN 256 * 8 equals LN T / (8 × LN 2) × 8 = LN T / LN 2. At each step, if 2^J exceeds the remaining value T, a "0" is printed; otherwise T is decremented by 2^J and "1" is printed. This is a classic non-restoring binary conversion by successive subtraction.

Note that this loop reuses the variable T (which holds the original decimal number) as the running remainder, so the original value is destroyed during conversion. This is not a bug in terms of output correctness, but means T cannot be reused after the loop without re-inputting.

Binary-to-Decimal Conversion (Lines 600–690)

The user enters a binary number as a string A$. The loop at lines 650–670 iterates over character positions from right to left using the idiom:

LET T=T+VAL A$(LEN A$-J)*2**J

Here A$(LEN A$-J) extracts a single character (using the Sinclair single-character substring notation), VAL converts it to 0 or 1, and it is multiplied by the appropriate power of 2. The accumulator T is initialised to 0 at line 600 but is not reset between conversions — this is a bug. On the second and subsequent iterations of the outer loop, T will retain its previous value, causing incorrect decimal results.

Notable Techniques

  • Logarithm for bit-width: Using LN T / LN 256 * 8 as a compact expression for ⌊log₂(T)⌋ is a neat mathematical trick that avoids a separate loop to find the highest set bit.
  • String variable reuse: B$ and C$ store repeated prompt strings, saving memory on repeated PRINT calls.
  • Single-character substring with VAL: VAL A$(N) on a single character is a concise way to convert a binary digit character to its numeric value.
  • 2**J notation: Uses the ** exponentiation operator rather than ^, which is the ZX81/TS1000 form.

Bugs and Anomalies

LineIssueEffect
600T reset only on first entry; not reset at top of inner loop (line 610)Second and subsequent binary inputs accumulate into a non-zero T, giving wrong decimal results
160If T=0 is entered, LN T produces an error (log of zero is undefined)Program crashes on input of 0
70No validation on base selection inputAny value other than 2 falls through to the decimal-to-binary path (since only T=2 is tested)

Variable Summary

VariablePurpose
B$Prompt string “BASE 10 NO.=”
C$Prompt string “BASE 2 NO.=”
TInput number / running remainder (decimal→binary) or accumulator (binary→decimal)
JLoop counter (bit position)
A$Binary string input by user

Content

Appears On

Assembled by Tim Ward from many sources. Contains programs 10001 – 10050.

Related Products

Related Articles

Related Content

Image Gallery

Number Base Conversion

Source Code

   5 REM %N%R% %B%A%S%E% %C%O%N%V%E%R%S%I%O%N
  10 LET B$="BASE 10 NO.="
  20 LET C$="BASE 2 NO.="
  50 PRINT "NUMBER BASE CONVERSION"
  60 PRINT ,,"CHANGING FROM WHICH BASE?","2 OR 10?"
  70 INPUT T
  80 CLS 
  90 IF T=2 THEN GOTO 600
 110 PRINT 
 120 PRINT ,,B$;
 130 INPUT T
 140 PRINT T
 145 PRINT C$;
 160 FOR J=INT (LN T/LN 256*8) TO 0 STEP -1
 170 IF 2**J>T THEN GOTO 210
 180 LET T=T-2**J
 190 PRINT "1";
 200 GOTO 220
 210 PRINT "0";
 220 NEXT J
 230 GOTO 110
 600 LET T=0
 610 PRINT 
 620 PRINT ,,C$;
 630 INPUT A$
 640 PRINT A$
 650 FOR J=0 TO LEN A$-1
 660 LET T=T+VAL A$(LEN A$-J)*2**J
 670 NEXT J
 680 PRINT B$;T
 690 GOTO 600
 700 SAVE "1004%3"
 800 RUN 

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.

People

No people associated with this content.

Scroll to Top