--- title: "Number Base Conversion" id: 57019 type: "computer_media" slug: "number-base-conversion" url: "http://localhost/computer_media/number-base-conversion/" markdown_url: "http://localhost/computer_media/number-base-conversion.md" published_at: "2024-10-02T07:42:06+00:00" modified_at: "2026-03-30T21:20:07+00:00" author: "David Anderson" featured_image: url: "http://localhost/wp-content/uploads/2024/10/43_Base.png" excerpt: "A bidirectional base-2/base-10 converter that uses logarithms to find the highest bit and processes binary strings character by character." category: - name: "Archived Media" slug: "archived-media" taxonomy: "category" url: "http://localhost/category/archived-media/" post_tag: - name: "Downloadable" slug: "downloadable" taxonomy: "post_tag" url: "http://localhost/tag/downloadable/" - name: "TS 1000" slug: "ts1000" taxonomy: "post_tag" url: "http://localhost/tag/ts1000/" model: - name: "Timex/Sinclair 1000" slug: "ts-1000" taxonomy: "model" url: "http://localhost/model/ts-1000/" genre: - name: "Mathematics" slug: "mathematics" taxonomy: "genre" url: "http://localhost/type/mathematics/" media_contents: - id: 56732 title: "Timex Sinclair Public Domain Library Tape 1001" type: "computer_media" url: "http://localhost/computer_media/timex-sinclair-public-domain-library-tape-1001/" media_type: "Program" mediadate: "198x" images: - url: "http://localhost/wp-content/uploads/2024/10/43_Base.png" media_type_tags: "Mathematics" --- # Number Base Conversion 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 | Line | Issue | Effect | | --- | --- | --- | | 600 | `T` 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 | | 160 | If `T=0` is entered, `LN T` produces an error (log of zero is undefined) | Program crashes on input of 0 | | 70 | No validation on base selection input | Any value other than 2 falls through to the decimal-to-binary path (since only `T=2` is tested) | ### Variable Summary | Variable | Purpose | | --- | --- | | `B$` | Prompt string “BASE 10 NO.=” | | `C$` | Prompt string “BASE 2 NO.=” | | `T` | Input number / running remainder (decimal→binary) or accumulator (binary→decimal) | | `J` | Loop counter (bit position) | | `A$` | Binary string input by user | ## 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 ```