You are on page 1of 16

BASIC PL/1 BIT DATA

MODULE 11
BIT Data
MODULE 11 Page 0 of 12
BASIC PL/1 BIT DATA
DECLARING BIT VARIABLES
We have used our frst STRING data type - CHAR - extensively so far, but BIT
the other STRING type - has been neglected. In this session we'll look at its
uses and the techniques for processing it. We'll also need to look closely at
some performance considerations that only really apply to the use of BIT.
DCL B_STR BIT(8);
In the above, B_STR is a BIT string of length 8 (and 8 bits occupy 1 byte of
storage = 1 character).
The Maximum length for a BIT variable (or constant) is 32767 bits, which is
about 8000 bytes. In the vast majority of cases we will be using BIT values of
length 1 (as we have so far), 8 and 16 - ie single bits, and 1-byte and 2-byte
values.
MODULE 11 Page 1 of 12
BASIC PL/1 BIT DATA
OPERATIONS ON BIT VARIABLES
We can assign and compare BIT values in the same way that we can
CHAR values:
DCL B_STR_1 BIT(8);
DCL B_STR_2 BIT(8);
B_STR_1 = B_STR_2;
IF B_STR_1 = B_STR_2
THEN ...
IF B_STR_1 = '00010001'B
THEN ...
If we have diferent lengths when we compare or assign, then we have
padding and truncation in the same way as with CHAR, but using '0'B instead
of '^'(space).
DCL B_STR_1 BIT(8);
DCL B_STR_2 BIT(16);
Example 1:
B_STR_1 = '1'B;
Here the RHS is shorter than the target variable. In this case, the value on the
LHS is padded on the right with '0'B bits to the length of the variable on the
left. The above assignment is equivalent to
B_STR_1 = '10000000'B;
Example 2:
B_STR_1 = B_STR_2;
Here the RHS gives a length greater than the target. The RHS value is
therefore truncated to the length of the LHS. The value assigned to B_STR_1
will be the frst 8 bits of B_STR_2.
Example 3:
B_STR_2 = B_STR_1;
MODULE 11 Page 2 of 12
BASIC PL/1 BIT DATA
As with Example 1, the value on the RHS is padded with '0'B to give the length
of the LHS. In this case the value assigned to B_STR_2 will be the value of
B_ST_1 followed by '00000000'B.
MODULE 11 Page 3 of 12
BASIC PL/1 BIT DATA
Example 4:
IF B_STR_1 = B_STR_2
THEN...
The comparison is performed in the same way as for CHAR data. The shorter
value is imagined to be padded on the right with enough '0'B (8 in this case) to
make it the same length as the longer value. The comparison then proceeds
bit by bit from the left. '1'B is classifed as being greater than '0'B.
MODULE 11 Page 4 of 12
BASIC PL/1 BIT DATA
ALIGNMENT CONSIDERATIONS
PL/1 has data attributes ALIGNED and UNALIGNED (UNAL for short). These
do not refer to the type of data that the variable can contain, but to the way in
which PL/1 will hold that data in storage.
ALIGNED means "store the data in such a way as to maximise the speed of
access at the risk of occupying more storage"
UNAL means "store the data in such a way as to minimise the storage
required, at the risk of increasing the time taken to access the data"
This is perhaps somewhat historical, going back to the days when saving a few
bytes in your program was really important. These days, within reason, the
size of a program is not signifcant - but its speed is.
Both the ALIGNED and UNAL attributes can be specifed for a single data
item, but they can also be specifed for a structure. In this case they are
inherited by the elements of the structure, unless we explicitly change that.
Example:
DCL 1 MAJOR_1 ALIGNED,
MINOR_1,
! EL1 "NAL,
! EL2,
MINOR_2 "NAL,
! EL,
! EL# ALIGNED;
Here, ALIGNED has been put on the major structure. So all elements are now
by default ALIGNED. On EL1 we have explicitly specifed UNAL, so it is UNAL
while EL2 is ALIGNED. On MINOR_2 we have specifed UNAL, and so that
overrides the ALIGNED on MAJOR_1. So EL3 is UNAL, but EL4 has been
explicitly made ALIGNED.
The defaults and efect for alignment for the variable types we've seen so far
are as shown below:
Data type Default Effect when Effect when
ALIGNED UNALIGNED
CHAR "NAL Nearest byte Nearest byte
MODULE 11 Page 5 of 12
BASIC PL/1 BIT DATA
$IC "NAL Nearest byte Nearest byte
FI%ED DEC ALIGNED Nearest byte Nearest byte
FI%ED BIN
(1&1!) ALIGNED Nearest halfword Nearest byte
(16&1) ALIGNED Nearest fullword Nearest byte
BIT "NAL Nearest byte Nearest bit
Note that for CHAR, PIC and FIXED DEC there is no diference between the
two.
For FIXED BIN, we should try to use ALIGNED wherever possible, as there is
a small time penalty in processing UNAL. Most of the records and segments
that we process will have been set up so that we can use ALIGNED (or let it
default).
The potential problem comes with BIT. The diference in speed of processing
between ALIGNED and UNAL can be enormous. We always try to ensure that
our BIT variables are ALIGNED. If they are in structures, then they will
probably be in complete bytes anyway, so we'll declare them as multiples of 8
and make them ALIGNED.
MODULE 11 Page 6 of 12
BASIC PL/1 BIT DATA
LOGICAL OPERATORS
We have seen comparisons and assignments performed on BIT values, and
these behave basically in the same way that they did on CHAR values. There
are three operators which work only on BIT values: they are AND (&), OR (|)
and NOT()
AND - WRITTEN AS &
This requires two operands, which must both be BIT values. The result of the
operation is also a BIT value. Consider
A ' B
where A and B are both BIT values (either constant or variable). & is like the
comparison operators, in that its two operands must be of the same length.
The shorter of A and B is therefore considered to be extended (in the same
way as with comparisons) to the length of the longer by padding on the right
with '0'B.
The actual operation is performed as follows:
The result of the operation is a BIT value of the same length as the
operands. Each BIT of the result is derived from the corresponding BITs of
the two operands according to the following table:
FIRST SECOND RESULT
OPERAND OPERAND BIT
BIT BIT
'0'B '0'B '0'B
'0'B '1'B '0'B
'1'B '0'B '0'B
'1'B '1'B '1'B
or in summary - the result string will have a '1'B bit in those positions where
both of the operands had '1'B bits. In all other places it will have '0'B bits.
MODULE 11 Page 7 of 12
BASIC PL/1 BIT DATA
OR - WRITTEN AS |
This works in exactly the same way as does AND, except for the table to
produce the result:
FIRST SECOND RESULT
OPERAND OPERAND BIT
BIT BIT
'0'B '0'B '0'B
'0'B '1'B '1'B
'1'B '0'B '1'B
'1'B '1'B '1'B
or in summary - the result string will have a '0'B bit in those positions where
both of the operands had '0'B bits. In all other places it will have '1'B bits.
The efect of AND and OR can best be seen by examples using constants:
OPERATION RESULT
'1100'B ' '1010'B '1000'B
'1100'B ( '1010'B '1110'B
'1100'B ' '1'B '1000'B
'1100'B ( '1'B '11OO'B
REMEMBER: Always try to make the operands of
& and I ALIGNED.
NOT - WRITTEN AS
This is a prefx operator, it only has one operand, which it precedes. Its result
is a string of length the same as the length of the operator, but with every '1'B
bit changed to '0'B, and every '0'B bit changed to '1'B.
OPERATION RESULT
'1100'B '0011'B
MODULE 11 Page 8 of 12
BASIC PL/1 BIT DATA
'0'B '1'B
MODULE 11 Page 9 of 12
BASIC PL/1 BIT DATA
REVISED PRIORITY CHART
Unary + and unary -
and** (NOT and Exponentiation)
* and / (multiply and divide)
+ and - (add and subtract)
|| (concatenation)
=,<,.. etc. (comparisons)
& AND
I OR
As you can see, the logical operators & and I have the lowest priority of all the
operators, with I being lower than &. So that an expression such as:
A ) B * C ) D ' $ + ,
(if one were foolish enough to code it without parentheses) would be
equivalent to:
((A ) B) * (C ) D) ) ' ($ + ,)
ie, the result of ANDing together the two comparison results (which are of
course both BIT values and therefore suitable for ANDing).
If you look at the priority chart, you will see that it is logical, because the
arithmetic operations will be done frst, then their results used in comparison
operations, and then the results of the comparison operations used as the
operands to AND and OR
However, it is worth using parentheses wherever there is any doubt about the
sequence in which operations will be done.
MODULE 11 Page 10 of 12
BASIC PL/1 BIT DATA
COMPLEX IF STATEMENTS
As we tend to use comparisons in IF statements- they give BIT values- the
logical operators take BIT operand and produce a BIT result, and the IF
statement requires a BIT value to test. So it seems logical to use the logical
operators to allow us to code more complicated IF tests.
IF A=B ' C=D
THEN ...
Here we have a 3-operator expression as the test for the IF statement. It will
be worked out in the sequence:
A=B gives a 1-bit result
C=D gives a 1 -bit result
the two 1-bit results are ANDed together to give a 1-bit result the IF tests this
1-bit result
This is an easy way to include more complex tests in our IF statements. There
are hidden dangers, however, for which we must watch out. Take the following
example:
DCL S-S BIT(8);
DCL A FI%ED DEC(!);
DCL B FI%ED DEC(!);
S-S = '11110000'B;
A = 12;
B = 1!;
IF S-S
THEN ... ./TR"E/.
IF A+B
THEN ... ./TR"E/.
IF A+B ' S-S
THEN ... ./TR"E/.
but if we had instead:
S-S = '00001111'B;
MODULE 11 Page 11 of 12
BASIC PL/1 BIT DATA
then the results would have been:
MODULE 11 Page 12 of 12
BASIC PL/1 BIT DATA
IF S-S
THEN ... ./TR"E/.
IF A +B
THEN ... ./TR"E/.
IF A +B ' S-S
THEN ... ./FALSE/.
This comes about because the & is not an English & but a true logical If we
see how the expressions are evaluated we will understand:
A 12, B 1!, S-S '11110000'B
A+B GI0ES '1'B
'1' ' '11110000'B GI0ES '10000000'B
'10000000'B IN THE IF GI0ES A TRUE RES"LT
A 12, B 1!, S-S '00001111'B
A+B GI0ES '1'B
'1' ' '00001111'B GI0ES '00000000'B
'00000000'B IN THE IF GI0ES A FALSE RES"LT
MODULE 11 Page 13 of 12
BASIC PL/1 BIT DATA
USE OF BIT MASKS
If our incoming records contain BIT variables acting as switches, then we will
need at times to be able to access them. If we assume that for efciency the
record has felds declared, say, as BIT(8) ALIGNED then we need a way of
testing one or more bits in the middle of a string:
Example:
DCL 1 IDN_REC,
REC_T1$E CHAR(1),
STATES BIT(8) ALIGNED,
./ 1 $A1MENT MADE /.
./ 2 $A1MENT RECORDED /.
./ INTEREST $AID /.
./ # ACCO"NT CLOSED /.
./ ! ARREARS NOTED /.
./ 6 BAILIFFS CALLED /.
./ 2 $ROSEC"TION /.
./ 8 CON0ICTION /.
Here a single variable STATES has 8 separate bits of information in it. If we
want to see if the interest has been paid, then there are various ways to go
about it. We are allowed to use SUBSTR on BIT strings, but this is potentially
inefcient, especially if we use a variable as the start position. More efcient is
the technique of BIT masking, which relies on the use of the logical operators
and the fact that an IF statement will accept a '1'B bit anywhere in the test
value. Consider the following coding:
IF STATES ' '001'B
THEN ...
We take the 8-bit value of STATES, and & it with a constant value. The result
win be a BIT string of length 8 (the length of STATES). The value will be:
'00?00000'B. The frst two and the last fve bits of the result will be '0'B,
because those bits have been ANDed with '0'B. The value of the third bit, ?,
will be the value of the third bit in STATES, because that has been ANDed with
'1' and will therefore be the same. If we test the result in our IF statement,
then the test would be TRUE if the third bit had been '1'B, and FALSE if it had
been '0'B.
This type of test operates very quickly, and is therefore preferred.
MODULE 11 Page 14 of 12
BASIC PL/1 BIT DATA
If we had a lot of testing to do on this feld, we might like to set up a series of
variables with these masking values built in:
DCL $_MADE BIT(8) ALIGNED;
$_MADE = '1'B;
DCL $_REC BIT(8) ALIGNED;
$_REC = '01'B;
Now we can make our tests easier to read, by coding:
IF STATES ' $_MADE
THEN ...
IF (STATES ' $_REC)
THEN ...
Life is not so easy if we have to make more complex tests. To test to see if
either the payment has been made or the payment recorded we need to code:
IF (STATES ' $_MADE)((STATES ' $_REC)
THEN ...
OR
IF STATES ' ($_MADE ( $_REC)
THEN ...
but if we need to test to see if they have both been made, we cannot code:
IF STATES ' $_MADE ' $_REC THEN ...
because this would always give a FALSE result.
We need to code:
IF (STATES '($_MADE ( $_REC)) =
($_MADE ( $_REC)
THEN ...
OR
IF (STATES ' '11'B) '11'B
THEN ...
MODULE 11 Page 15 of 12

You might also like