This program is an interactive binary number visualizer that displays how decimal values from 0 to 255 are represented in 8-bit binary. It draws colored rectangular columns on screen — one per bit — using PAPER color changes (green or red) to indicate whether each bit is 0 or 1, and labels each column with its positional power of two. The program offers two modes: a Calculate mode where the user enters any decimal value and sees its binary representation, and a Demonstration mode that automatically counts from 1 to 255, cycling through each representation while playing a tone via BEEP scaled to the current value. A decimal readout is formatted to always display three digits using string slicing on a zero-padded STR$ conversion.
Program Analysis
Program Structure
The program is organized into clearly separated subroutines, each prefaced with REM comment blocks. Control flow is as follows:
- Line
110: Initialization — calls the screen-setup subroutine at400, setsnumber=0, then calls the display subroutine at300. - Lines
120–160: Main menu loop — waits for keypress"c"or"d"and dispatches to the appropriate subroutine. - Lines
300–390: Binary display subroutine — decomposesnumberinto 8 bits and renders them visually. - Lines
400–460: Screen setup subroutine — draws the static frame, column separators, and labels. - Lines
500–550: Demonstration subroutine — loops from 1 to 255, calling display for each value. - Lines
600–640: Calculate subroutine — accepts user INPUT and validates the range before displaying.
Binary Decomposition Algorithm
The subroutine at line 300 uses a classic successive-subtraction approach rather than bitwise operations, which BASIC does not natively support. Starting with power=128, it subtracts the current power from remain; if the result goes negative, it restores remain and sets result=0, otherwise result=1. The power is halved each iteration through LET power=power/2.
Visual Rendering
Each of the 8 bit columns is drawn as a tall rectangle using a FOR y=3 TO 9 loop, printing two spaces with a PAPER color of either 4 (green) for bit=1 or 2 (red… actually cyan at code 5, but here 4-2*result gives PAPER 4 for result=1 and PAPER 4… wait — 4-2*1=2 (red) and 4-2*0=4 (green)).
More precisely: LET color=4-2*result yields color=2 (red) when the bit is 1, and color=4 (green) when the bit is 0. This is an inverted color mapping — active bits appear red and inactive bits appear green. The column horizontal position is computed as z=x*4+1, spacing each 4-character-wide column evenly across the 32-column display.
Vertical separator lines between bit columns are drawn using PLOT/DRAW in the screen-setup subroutine at line 420, operating in pixel coordinates to create graphics overlaid on the text grid.
Decimal Readout Formatting
Line 380 uses a compact zero-padding idiom:
LET a$="00"+STR$ numberprepends two zeros to the string representation of the number.LET a$=a$(LEN a$-2 TO LEN a$)then slices the last three characters, guaranteeing a fixed-width three-digit display regardless of whether the value is 1, 2, or 3 digits wide.
Demonstration Mode
The demonstration loop at lines 520–550 iterates a from 1 to 255, assigning to Number (note the capital N — this is a distinct variable from number used elsewhere, due to case-sensitivity). This is a latent bug: the display subroutine at line 300 reads number (lowercase), so LET Number=a at line 520 never actually updates the value displayed. The demonstration would therefore show number=0 for all iterations, or whatever value number held from a previous operation.
The freeze feature in demonstration mode works by polling INKEY$ at line 540: if any key other than "m" is held, the loop spins on line 540 until the key is released before advancing to the next value.
Audio Feedback
Line 390 plays a tone with BEEP .5,number/4, scaling the pitch to the current decimal value divided by 4. This produces a rising pitch as larger values are displayed, providing an auditory cue correlated to the magnitude of the number.
Key Variables
| Variable | Purpose |
|---|---|
number | The decimal value (0–255) being visualized |
Number | Intended demo counter (case mismatch bug — not read by display subroutine) |
power | Current bit weight (128 down to 1) |
remain | Remainder during bit extraction |
result | Current bit value (0 or 1) |
x | Bit column index (0–7) |
z | Screen column position for current bit |
color | PAPER color index for bit rectangle |
a$ | Reused for keypress detection and formatted decimal string |
Notable Anomaly
The reuse of a$ both as a keypress variable in the main menu loop (lines 130–160) and as the formatted decimal string in the display subroutine (line 380) is harmless given the call structure, but reduces readability. More significantly, the case mismatch between Number (line 520) and number (line 310) means the demonstration mode does not function as intended.
Content
Source Code
10 REM
11 REM program
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" THEN GO SUB 500: GO TO 120
150 IF 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 14,10; INK 7; PAPER 1;"DATA BUS"
440 PRINT AT 13,0;"(";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" 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 zero 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
9999 SAVE "BINARY NO." LINE 1
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.
