Pascal set type – HP SunSoft Pascal 4.0 User Manual

Page 134

Advertising
background image

110

Pascal 4.0 User’s Guide

6

Pascal Set Type

In Pascal, a set type is implemented as a bit vector, which is similar to a C
short-word array. Direct access to individual elements of a set is highly
machine-dependent and should be avoided.

In Pascal, bits are numbered within a byte from the most significant to least, as
shown in Table 6-3.

In C, a set could be described as a short-word array beginning at an even
address. With the current set representation, it does not matter what the
lower-bound value is.

The nth element in a set

[lower...upper]

can be tested as follows:

Table 6-3

Set Implementation

Set

Bit Numbering

set+3:

31, 30, 29, 28, 27, 26, 25, 24

set+2:

23, 22, 21, 20, 19, 18, 17, 16

set+1:

15, 14, 13, 12, 11, 10, 9, 8

set+0:

7, 6, 5, 4, 3, 2, 1, 0

#define LG2BITSLONG 5 /* log2( bits in long word) */

#define LG2BITSWORD 4 /* log2( bits in short word) */

#define MSKBITSLONG 0x1f

#define MSKBITSHORT 0x0

short *setptr; /* set as array of shorts */

int upper; /* upper bound of the set */

int elem; /* ordinal value of set element */

int i;

if ( setptr[elem >> LG2BITSWORD] &

(1 << (elem & MSKBITSWORD)) ) {

/* elem is in set */

}

Advertising