Computer Number Systems

Author(s): William Herron
Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v2 n2 (Volume 2, Issue 2)
Date: February 1981
Pages: 10-11

Your ZX80 or MicroAce computes for you in decimal numbers, the number system we all learned to count in. But it thinks in another number system, called binary. Instead of 10 digits, 0-9, in the decimal system, binary has only 2–0 and 1 (hence the name binary).

When you see a decimal number such as 24, you know it means 2 tens (twenty) plus 4 ones (four). Another way to look at it is 2×10¹ plus 4×10⁰. Each place represents a power of 10. Binary works the same way, except that each place represents a power of 2. For example, 1011 means 1×2³ plus 0x2² plus 1×2¹ plus 1×2⁰, for a total of 11 in decimal. (Remember, any number to the zero power=1)

Why is this useful? All digital computers, like the ZX80, work in this base 2 system. The ZX80 converts everything to binary digits (or “bits”) and handles them in 8-bit units, called bytes.

Each keystroke uses 1 byte of memory space in your computer. A ZX80 comes with 1K memory, and some MicroAces with 2K. This means you have 1024 (2¹⁰) bytes available in your ZX80, 2048 (2¹¹) in your MicroAce. (Computer users round to 1K and 2K respectively. The 16K RAM will really have 16384 (2¹⁴) bytes of memory available.)

Before you can get into the gut workings of your computer, you must understand that it works in a different number system than you do. Later we’ll see how you can use binary numbers to decode addresses in memory or manipulate graphics (for example, Big Characters–see page 3–reads the binary representation of each line of a character’s video image and expands it).

These 2 programs, by Bill Herron of San Diego, convert decimal numbers to binary and back. In later issues Bill and I will show you how to convert to hexidecimal (base 16) and octal (base 8) numbers, other number systems used with computers. To run the DEC to BI program again, just hit NL. To exit, hit any key then NL. To run the BI to DEC program again, change line 430 to RUN. To exit, hit NL then BREAK while the screen is blanked.

   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
Syntactic Sum= 14416

NOTE–Range: -32768≤D≤32767; 0₂≤BI≤111111111111111₂

 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
Syntactic Sum= -3481

NOTE–Range: 0≤DEC≤268435455; 0₂≤BI≤1111111111111111111111111111₂

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