Shows how a decimal number from 0 to 255 is written in binary. The screen has a row of eight boxes, each labelled with its place value from 128 down to 1. When a number is entered, the program fills each box with a 1 or a 0, clears the place values of the 0 bits, and prints the decimal and binary forms with a note that the place values still showing add up to the number. Pressing ENTER starts again.
Program Analysis
Program Structure
Lines 1000–1190 draw two rows of eight boxes from block-graphic strings, using subroutine 1200 to print each 4-character segment eight times. Lines 1300–1320 print the place values 2**N under the boxes. The conversion loop (lines 2100–2160) tests N against each power of two from 27 down, prints a 1 or 0 in the top row, blanks the place value when the bit is 0, adds the digit to C$, and subtracts the place value when the bit is set.
Notable Techniques
- Clearing the place values of the 0 bits leaves the decomposition on screen, so the user can see which powers of two make up the number.
- Input is taken in
SLOWmode and the display work is done inFAST. Values outside 0–255 are refused and the prompt is repeated.
Source Code
1000 CLS
1010 FAST
1020 PRINT AT 1,7;"█[B][I][N][A][R][Y]█[T][E][A][C][H][E][R]█"
1030 LET A$="▛▀▀▀"
1040 PRINT AT 8,0;
1050 GOSUB 1200
1060 PRINT AT 9,0;
1070 LET A$="▌ "
1080 GOSUB 1200
1090 LET A$="▛▀▀▀"
1100 PRINT AT 10,0;
1110 GOSUB 1200
1120 LET A$="▌ "
1130 PRINT AT 11,0;
1140 GOSUB 1200
1150 LET A$="▀▀▀▀"
1160 PRINT AT 12,0;
1170 GOSUB 1200
1180 PRINT AT 12,0;AT 8,31;"▜";AT 9,31;"▐";AT 10,31;"▜";AT 11,31;"▐";
1190 GOTO 1300
1200 FOR N=1 TO 8
1210 PRINT A$;
1220 NEXT N
1230 RETURN
1300 FOR N=7 TO 0 STEP -1
1310 PRINT AT 11,29-(N*4);2**N
1320 NEXT N
1330 LET C$=""
2000 PRINT AT 21,0;"INPUT A NUMBER BETWEEN 0 AND 255";
2010 SLOW
2020 INPUT N
2025 FAST
2030 IF N<0 OR N>255 THEN GOTO 2020
2040 LET NN=N
2100 FOR I=1 TO 8
2110 IF N>=2**(8-I) THEN LET B$="1"
2120 IF N<2**(8-I) THEN LET B$="0"
2130 PRINT AT 9,I*4-3;B$
2140 IF B$="0" THEN PRINT AT 11,I*4-3;" ";
2145 LET C$=C$+B$
2150 IF N>=2**(8-I) THEN LET N=N-2**(8-I)
2160 NEXT I
2170 PRINT AT 4,0;NN;" DECIMAL = ";C$;" BINARY"
2180 PRINT AT 16,0;"THESE NUMBERS ADDED TOGETHER EQUAL ";NN
2190 PRINT AT 11,31;"▐";AT 21,0;"PRESS ENTER TO RECYCLE ::: ";
2200 PAUSE 40000
2210 CLS
2220 RUN
9998 SAVE "BINARY TEACHE[R]"
9999 RUNNote: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.
