This program demonstrates binary number representation by displaying an 8-bit “data bus” on screen, where each bit position is shown as a coloured column that lights green (PAPER 4) or yellow (PAPER 6) depending on whether that bit is set. The bit-extraction routine at line 310–370 uses successive halving of a power-of-2 variable (starting at 128) and subtraction to determine each bit’s value without using bitwise operators, which are unavailable in Sinclair BASIC. A demonstration mode (subroutine 500) counts automatically from 1 to 255, while a calculate mode (subroutine 600) accepts direct decimal input and validates the range 0–255. The decimal value is displayed with a zero-padded two-digit format constructed by prepending “00” to the string and taking the last three characters, a common Sinclair BASIC padding idiom.
Program Structure
The program is organised into clearly separated subroutines, each prefaced with REM comment blocks:
| Lines | Role |
|---|---|
| 10–11 | Title REMs |
| 110–160 | Main loop: initialisation and menu dispatch |
| 300–390 | Core display routine: render binary bits and decimal value |
| 400–460 | Screen setup: borders, column dividers, labels |
| 500–550 | Demonstration mode: auto-increment 1–255 |
| 600–640 | Calculate mode: validated decimal input |
Execution begins at line 110, which calls the screen-setup subroutine (400) and then the display routine (300) with number=0 before entering the menu loop.
Bit-Extraction Algorithm (Lines 310–370)
Because Sinclair BASIC provides no bitwise AND or shift operators, the subroutine at line 310 implements manual bit decomposition using subtraction:
- Initialise
power=128andremain=number. - For each bit position
x(0 to 7): subtractpowerfromremain. - If
remaingoes negative, restore it by addingpowerback and setresult=0; otherwiseresult=1. - Halve
powerand advance to the next bit.
This is the standard “successive subtraction” equivalent of a right-shift and mask in assembly language.
Screen Layout and Graphics
The setup routine at line 420 uses a combined FOR loop to draw all eight bit columns in one pass. For each column x, it prints the bit label (d7 down to d0) and the corresponding power-of-two value, then uses PLOT/DRAW to draw vertical separator lines at pixel positions 32*x+7 and 32*x+24, spanning from y=96 to y=151 (55 pixels high). A horizontal rule is drawn at y=174 across the full data bus area, and another at y=64 beneath the binary digit row.
Each bit column occupies four character cells of width (z=x*4+1), giving eight columns across the 32-column screen with one column of margin on each side.
Colour Coding of Bits
Line 350 computes color=4-2*result, yielding PAPER 4 (green) when result=1 and PAPER 2 (red) when result=0. Wait — PAPER 4 is green and PAPER 2 is red in the standard Spectrum palette. Each lit column is printed over rows 3–9 (seven character rows), making the bit columns visually prominent.
Zero-Padded Decimal Display
Line 380 constructs a zero-padded three-character decimal string using the idiom:
LET a$="00"+STR$ number: LET a$=a$(LEN a$-2 TO LEN a$)
Prepending “00” and then slicing the last three characters produces “000”–”255″. This avoids conditional branching or IF chains to handle different digit counts.
Demonstration Mode (Lines 500–550)
The demo loop iterates a from 1 to 255, calling the display subroutine each time. A freeze feature is implemented at line 540: if any key other than M/m is held, INKEY$ is non-empty and the program busy-waits in a tight GO TO 540 loop until the key is released. Pressing M or m exits the subroutine. Note that the loop does not reset to 0 after reaching 255; it simply returns, leaving the display showing 255.
Input Validation (Lines 620–630)
User input is taken with INPUT and then truncated with INT to discard any fractional part. The condition number>255 OR number<0 re-prompts for out-of-range values. However, no protection is given against non-numeric input, which would cause a BASIC error rather than a graceful re-prompt.
BEEP Usage
Line 390 ends the display subroutine with BEEP .5,number/4, producing a half-second tone whose pitch scales with the current value. For number=0 this produces a 0-semitone (middle C) tone; for 255 it produces a tone 63.75 semitones above middle C. This provides audio feedback in both demo and calculate modes.
Content
Source Code
10 REM Binary demonstration
11 REM program
100 REM
101 REM set up
103 REM
110 GO SUB 400: LET number=0: GO SUB 300
115 REM
116 REM Menu
117 REM
120 PRINT AT 20,1;"Press "; INVERSE 1;"C"; INVERSE 0;"alculate or "; INVERSE 1;"D"; INVERSE 0;"emonstrate"
130 LET a$=INKEY$: IF a$="" THEN GO TO 130
140 IF a$="d" OR a$="D" THEN GO SUB 500: GO TO 120
150 IF a$="c" OR a$="C" THEN GO SUB 600: GO TO 120
160 GO TO 130
300 REM
301 REM Display Binary Number
302 REM
310 LET power=128: LET remain=number
320 FOR x=0 TO 7
330 LET result=1: LET remain=remain-power
340 IF remain<0 THEN LET remain=remain+power: LET result=0
350 LET z=x*4+1: LET color=4-2*result: FOR y=3 TO 9: PRINT AT y,z; PAPER color;" ": NEXT y
360 PRINT AT 12,z;result
370 LET power=power/2: NEXT x
380 LET a$="00"+STR$ number: LET a$=a$(LEN a$-2 TO LEN a$): PRINT AT 17,22;a$
390 BEEP .5,number/4: RETURN
400 REM
401 REM Display Screen
402 REM
410 BORDER 4: PAPER 6: INK 1: CLS
420 LET power=256: FOR x=0 TO 7: LET power=power/2: LET z=x*4+1: PRINT AT 1,z;"d";7-x;AT 2,z; INK 3;power: PLOT 32*x+7,96: DRAW 0,55: PLOT 32*x+24,96: DRAW 0,55: NEXT x
430 PRINT AT 0,0;"(";AT 0,31;")": PLOT 6,174: DRAW 244,0: PRINT AT 0,12; INK 7; PAPER 1;"DATA BUS"
440 PRINT AT 13,0;"(";AT 13,31;")": PLOT 6,64: DRAW 244,0: PRINT AT 14,10; INK 7; PAPER 1;"BINARY NUMBER"
450 PRINT AT 17,5;"Decimal Number--"
460 RETURN
500 REM
501 REM Demonstration
502 REM
510 PRINT AT 20,1;"Press "; INVERSE 1;"M"; INVERSE 0;"enu or hold down "; INVERSE 1;"F"; INVERSE 0;"reeze"
520 FOR a=1 TO 255: LET number=a: GO SUB 300
530 IF INKEY$="M" OR INKEY$="m" THEN RETURN
540 IF INKEY$<>"" THEN GO TO 540
550 NEXT a: RETURN
600 REM
601 REM Calculate
602 REM
610 PRINT AT 20,1;"Enter a value from 0 to 255 "
620 INPUT "Decimal Number ? ";number
630 LET number=INT number: IF number>255 OR number<0 THEN GO TO 620
640 GO SUB 300: RETURN
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.
