Decimal to Binary and Binary to Decimal

Developer(s): William Herron
Date: 1981
Type: Program
Platform(s): ZX80

Two short programs by Bill Herron. Decimal to Binary asks D= for a number from -32768 to 32767 and prints its 16 bits; press NEWLINE for another number, or any other key and NEWLINE to stop. Binary to Decimal asks BI= for up to 28 binary digits and prints DEC=, the decimal value. It ends with CLEAR, so RUN it again for another number.

The article explains binary place values and how the ZX80 stores numbers. The download has both programs.

Published in Syntax February 1981 (Computer Number Systems).

Related Articles

Introduction to binary with programs to convert from decimal to binary and binary to decimal.

Image Gallery

Source Code

   1 REM DECIMAL TO BINARY
 100 PRINT "D=";
 110 INPUT D
 120 PRINT D
 200 PRINT "BI=";
 205 LET MSK1=-32767-1
 210 FOR Z=1 TO 16
 220 PRINT (D AND MSK1)/MSK1;
 230 LET MSK1=ABS(MSK1/2)
 240 NEXT Z
 250 PRINT
1000 INPUT Z$
1020 IF Z$="" THEN GO TO 100

 100 REM BINARY TO DECIMAL
 110 DIM X(14)
 112 DIM Y(14)
 114 LET A=10**4
 120 LET X(0)=2**13
 130 FOR I=1 TO 14
 140 LET X=X(I-1)*2
 142 LET C=X/A
 144 LET X(I)=X-C*A
 150 LET Y(I)=Y(I-1)*2+C
 160 NEXT I
 170 DIM B(27)
 200 PRINT "BI=";
 204 INPUT B$
 206 LET F=0
 210 FOR I=0 TO 27
 220 LET B=CODE(B$)
 230 IF B=1 THEN GO TO 292
 234 IF B=29 THEN LET F=1
 240 IF B<30 AND B>27 AND F>0 THEN GO TO 270
 250 LET B$=TL$(B$)
 260 GO TO 220
 270 LET B(I)=B-28
 275 PRINT B(I);
 280 LET B$=TL$(B$)
 290 NEXT I
 292 LET S=I-1
 294 IF S<0 THEN GO TO 297
 295 PRINT
 296 GO TO 300
 297 LET S=0
 298 PRINT S
 300 LET U=0
 305 LET L=0
 320 FOR I=0 TO S
 330 IF B(I)=0 THEN GO TO 380
 340 IF S-I>12 THEN GO TO 350
 342 LET X=2**(S-I)
 344 LET Y=0
 346 GO TO 360
 350 LET X=X(S-I-13)
 352 LET Y=Y(S-I-13)
 360 LET L=L+X
 362 LET C=L/A
 364 LET L=L-C*A
 370 LET U=U+Y+C
 380 NEXT I
 400 PRINT "DEC=";
 410 IF U>0 THEN GO TO 413
 411 PRINT L
 412 GO TO 430
 413 PRINT U;
 420 PRINT TL$(STR$(L+A))
 430 CLEAR

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