Box Dept E Lake Havasu Az

DON'T USE INTEGER VARIA3LES (TWO BYTES) IF

ALL YOU NEED TO STORE IS 0 OR 1. THE TECHNIQUE DESCRIBED IN THIS ARTICLE SHOWS HOW TO CUT MEMORY REQUIREMENTS IN THIS SITUATION BY 15/16!

lavis, Sunnyvale, CA

vs BYTES

Computer programs for many different applications require maintaining tables wherein the values to be entered in the tables can only take on the values zero or one. Maze games are an example where the program may need to keep track of whether a wall segment is "open" or "closed". In a business application, it may be required to store data such as exempt vs. non-exempt, full time vs. part time, etc.

The most straightforward approach to storing this type of data is to set up integer arrays. In the business example cited, and array of integers dimensioned for the number of employees could be used for storing a zero or a one to indicate full time or part time status. Since each element of the integer array cosumes two bytes of memory, it doesn't take many employees or a very large maze to quickly exhaust the addressable memory in a TRS-80 or other Micro. In addition, reading or writing this data to tape or disk can also be time and space consuming.

However, since these integers can only take on the values of zero or one, one bit is all that is required for storing this data instead of the 16 required for an integer variable or integer array element. In order to make use of this possible data compacting, methods are required for testing whether a specific bit is zero or one and for setting a bit to zero or one. GOSUB 1 and GOSUB 2 perform these functions using a technique called bit masking and the logical operators available in TRS-80 Basic.

PROGRAM 1 demonstrates the conventional approach to storing and manipulating all of these zeros and ones. Lines 100-110 set an array element to zero; lines 200-210 set an element to one; and, lines 300-310 test an array element. PROGRAM 2 is PROGRAM 1 modified to make use of the bit-by-bit data storage technique. Note that in PROGRAM 1 the array NX has 3001 elements (numbered from 0 to 3000) at two bytes each for a total requirement of 6002 bytes. In PROGRAM 2, the NX array consumes only 376 bytes for a saving of 5626 bytes. Of course, the total memory saving is not quite this great because of the "overhead" which has to be paid in the form of including the two GOSUBs in the main program and the calculations which must be done before the GOSUBs can be called. However, if only one array needs to be manipulated in this way in a given program, the calculation of such variables as JK, NN, and J in PROGRAM 2 can be included as part of the GOSUBs and this will help to reduce the overhead. To determine the proper array dimension, divide the total number of desired locations by 16 and round upward. In the example, 3001 locations were desired, and 3001/16 = 187+ so 188 locations were necessary in PROGRAM 2, numbered zero through 187.

This technique can be expanded to simulate having large, multi-dimensional arrays as might be required for the hypothetical maze game. In addition, variables having values other than zero or one can be "packed" into a single integer using a variation of this technique and this may be covered in a future article.

8020 IF J = 15 THEN JJ = -32768 ELSE JJ = INT(2tJ + .5)

8030 IF II = 1 THEN NN = NN OR JJ ELSE NN = NN AND (NOT JJ)

0 0

Post a comment