Since the Sinclair-Timex, and many other computers, store characters in 8-bit bytes, it is commonly assumed that 8 bits are required to store a character of text. This is not so. There are methods, called data compression techniques, which allow you to store data in less storage space. The penalty is that it takes time to encode and decode your data.
The amount of data compression that can be achieved depends on the amount of a priori information known about the data. For example, in English text, the letter E occurs far more frequently that any other letter. Therefore, you could save space by using variable length codes with short codes for the letter E and other frequently used characters. In this article we will examine a method of data compression which does not make assumptions about the frequency of use of characters.
Information theory, a field pioneered by Claude E. Shannon in 1949, tells us that if we have r characters to choose from, then at least log₂ r bits are required to store a character. From the Sinclair-Timex user’s manuals, we see that there are approximately 256 different characters (a few are not assigned), with CODEs from 0 to 255. Thus, log₂ 256 = 8 bits are required to store an arbritary character.
Does this contradict with what we said earlier? Not if you note that most data uses only a small fraction of those 256 characters. Let’s consider text which uses: the digits 0 through 9; the three punctuation points — period, comma, and question mark; the letters A through Z; and the space character. The minimum storage for text containing just these 40 characters is log₂ 40 or approximately 5.3 bits. If we were able to pack the characters into their minimum required storage space, we could save almost 3 bits per character. But we have two problems. Firstly, we can’t store data in a fraction of a bit. And, if we use 6 bits for each character, we wastes about 13 percent of the storage space (what a different a bit makes). The second problem is that it is very inconvenient to manipulate 6 bits of data on our computer, even in machine language. The solution lies in bunching characters together. Three characters require 3*5.3 or approximately 15.9 bits. Using 16 bits for three characters doesn’t waste much space.
Following is a very flexible, and therefore fairly complex, program to do data compression. You may have difficutly figuring out how it works, thus it is probably best to treat it as a black box — us it as is.
- M$ is the character set, which in our example is the 40 characters cited above. You could replace these characters with those characters you need in your application, e.g., just the 10 decimal numbers.
- B is the number of bytes that receive one bunch of characters. Here we are packing 3 characters into two bytes, so B is 2. The program figures out N, the number of characters to pack into the B bytes, in this case three. You can find the percent efficiency of your packing by PRINTing 100*(N*LN M)/(8*B*LN 2), where M is LEN M$.
- I$ is the input characters (your data).
- The subroutine at line 200 compresses I$ and returns the result in A$. If the number of characters in I$ is not a multiple of N, the subroutine will pad I$ with the necessary extra character(s).
- The subroutine at line 800 decompresses I$ and puts the result in A$.
Each half of the program will run in 1 K RAM, if the REMs are deleted. (This may not be too useful, however, since there is little room left for data.) To run in a 1 K system:
1. Write two programs — one with the compress and one with the decompress subroutines.
2. Figure out what your data encodes into using compress.
3. Add this data string and the decompress subroutine to your final program.
If you are familiar with the PDP-11 computers, you probably have seen this compression technique under the name RADIX-50. Why 50? In octal (base 8), 50 is decimal 40.
1 REM SQUEEZE
2 LET B=2
3 LET M$="1234567890 ABCDEFGHIJKLMNOPQRSTUVWXYZ,.?"
10 DIM K(B)
20 LET T=256
30 LET M=LEN M$
40 LET N=INT (8*B*LN 2/LN M)
100 INPUT I$
110 GOSUB 200
120 FOR I=1 TO LEN A$
130 PRINT I;"=";A$(I);CODE A$(I),
140 NEXT I
150 STOP
200 REM
210 LET A$=""
220 IF LEN I$=N*INT (LEN I$/N) THEN GOTO 250
230 LET I$=I$+M$(M)
240 GOTO 220
250 LET I=0
260 IF I<>N*INT(I/N) THEN GOTO 300
270 FOR K=B TO 1 STEP -1
275 IF I>0 THEN LET A$=A$+CHR$ K(K)
280 LET K(K)=0
290 NEXT K
300 LET I=I+1
310 IF I>LEN I$ THEN RETURN
320 LET J=-1
330 LET J=J+1
340 IF J+1<M AND I$(I)<>M$(J+1) THEN GOTO 330
350 FOR K=1 TO B
360 LET K(K)=M*K(K)+J
370 LET J=INT (K(K)/T)
380 LET K(K)=K(K)-T*J
390 NEXT K
400 GOTO 260
800 REM DECOMPRESS
810 LET A$=""
820 LET I=0
830 LET I=I+B
840 IF I>LEN I$ THEN RETURN
850 FOR K=1 TO B
860 LET K(K)=CODE I$(I+1-K)
870 NEXT K
880 FOR J=1 TO N
890 LET R=0
900 FOR K=B TO 1 STEP -1
910 LET R=K(K)+T*R
920 LET K(K)=INT (R/M)
930 LET R=R-M*K(K)
940 NEXT K
950 LET A$=M$(R+1)+A$
960 NEXT J
970 LET A$=A$(N+1 TO )+A$( TO N)
980 GOTO 830
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.