Computer Number Systems III

Author(s): Don Richardson
Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v2 n4 (Volume 2, Issue 4)
Date: April 1981
Pages: 10

In the last 2 issues we learned about hexadecimal and binary numbers, 2 systems used by computers. Now we’ll look at octal numbers.

Octal, or base 8, numbers work just like binary, hex, or decimal numbers. Each digit represents a power of the base you’re working in, so an octal number is expressed as powers of 8. And as in decimal you have 10 digits and binary you have 2, in octal you have only 8 digits, 0-7.

Thus 12 in decimal stands for 1×10^1 plus 2×10^0. In octal, 12 means 1×8^1 plus 2×8^0, or 10 in decimal. (Remember that any number to the zero power=1.)

Octal numbers are most useful for computers whose word lengths are multiples of 3 (the ZX80 word is 8 bits). Also all old business machines, like mechanical adding machines, worked in base 8.

This conversion program by Don Richardson of Hampton, VA, translates numbers to any base up to base 36. Each digit of the new number represents a power of that number, so ABC_D=AxD^2+BxD^1+CxD^0. To run the program, press RUN (NL) and just answer the prompts, hitting (NL) after each. Do not ask it to convert to base 1. Because base 1 has only the digit 0, the only number you can practically represent is 0 and the computer will run interminably.

  10 CLS
20 PRINT "WHAT BASE? (ENTER 10 IF YOU WANT"
30 PRINT "TO GO FROM BASE 10 TO ANOTHER"
40 PRINT "BASE, OR ELSE ENTER THE BASE"
50 PRINT "TO CONVERT FROM)"
60 INPUT B
70 CLS
80 IF B=10 THEN GO TO 230
90 PRINT "WHAT IS THE NUMBER?"
100 INPUT A$
110 CLS
120 PRINT A$;" BASE ";B;"=";
125 LET PN=1+2*(CODE(A$)=220)
127 IF CODE(A$)=220 THEN LET A$=TL$(A$)
130 LET X=0
140 LET Y=CODE(A$)-28
150 IF Y<B THEN GO TO 180
160 PRINT "A NON-LEGAL NUMBER OF BASE ";B
170 GO TO 390
180 LET X=B*X+Y
190 LET A$=TL$(A$)
200 IF NOT A$=CHR$(1) THEN GO TO 140
210 PRINT PN*X;" BASE 10 "
220 GO TO 390
230 PRINT "WHAT BASE ARE YOU CONVERTING TO?"
240 INPUT B
250 CLS
260 PRINT "WHAT IS THE BASE 10 NUMBER?"
270 INPUT Y
280 CLS
290 PRINT Y;" BASE 10=";
295 IF Y<0 THEN PRINT "-";
297 LET Y=ABS(Y)
300 LET N=-1
310 LET N=N+1
320 IF NOT Y/B**N<B THEN GO TO 310
330 FOR I=0 TO N
340 LET X=B**(N-I)
350 PRINT CHR$(Y/X+28);
360 LET Y=Y-(Y/X)*X
370 NEXT I
380 PRINT " BASE ";B
390 PRINT
400 PRINT "DO YOU WANT ANOTHER CONVERSION?"
410 INPUT A$
420 IF CODE(A$)=62 THEN GO TO 10
SYNTACTIC SUM=-6933

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