A value occupying a machine word has the mode
BITS. The number of binary digits in one
machine word is given by the environment enquiry
(see section 13.2) bits
width which, for the a68toc
compiler is 32
. A BITS
value can be denoted
in four different ways using denotations written
with radices of 2, 4, 8 or 16. Thus the declarations
BITS a = 2r 0000 0000 0000 0000 0000 0010 1110 1101 BITS b = 4r 0000 0000 0002 3231 BITS c = 8r 000 0000 1355 BITS d = 16r0000 02ed
are all equivalent because they all denote the same value. Notice
that the radix precedes the r
and is
written in decimal. Notice also that the numbers can be written with
spaces, or newlines, in the middle of the number. However, you cannot
put a comment in the middle of the number. Since a machine
word contains 32 bits, each denotation should
contain 32 digits in radix 2, 16 digits in radix 4, 11 digits in radix
8 and 8 digits in radix 16, but it is common practice to omit digits
on the left of the denotation whose value is zero. Thus the
declaration for d
could have been written
BITS d = 16r2ed
When discussing values of mode BITS
where the values
of more significant bits is important, full denotations like the above
may be more appropriate.
There are many operators for BITS
values. Firstly, the
monadic operator BIN takes an
INT
operand and yields the equivalent value with mode
BITS
. The operator ABS
converts a BITS
value to its equivalent with mode
INT
. The NOT operator which
you first met in chapter 4
(section 4.2) takes a
BITS
operand and yields a BITS
value where
every bit in the operand is reversed. Thus
NOT 2r 1000 1110 0110 0101 0010 1111 0010 1101
yields
2r 0111 0001 1001 1010 1101 0000 1101 0010
Notice that spaces have been used to make these binary denotations
more comprehensible. NOT
is said to be a bit-wise
operator because its action on each bit is
independent of the value of other bits.
AND and OR
(both of which you also met in chapter 4) both take two
BITS
operands and yield a BITS
value. They
are both bit-wise operators and their actions are summarised as
follows:
Left Operand | Right Operand | AND | OR |
---|---|---|---|
0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 |
1 | 0 | 0 | 1 |
1 | 1 | 1 | 1 |
For OR
, the yield of
2r 100110 OR 2r 10101
is 2r 110111
. The priority of AND
is 3
and the priority of OR
is 2.
The AND
operator is particularly useful for extracting
parts of a machine word. For example, suppose you have a
BITS value where the least-significant 8
bits are equivalent to a character. You could write
CHAR c = REPR ABS (b AND 16rff)
Here, the operators REPR and ABS do not generate machine-code instructions, but merely satisfy the compiler that the modes are correct. This sort of formula is, in fact, very efficient in Algol 68.
It is possible to extract a single bit from a word using the operator ELEM which has the header
(INT n,BITS t)BOOL:
For example, given the declaration
BITS bi = 16r 394a 2716
then each hexadecimal digit represents 4 bits: the 3
occupies bit
positions 1-4, the 9
occupies bit positions 5-8, the
4
, bit positions 9-12, and so on. Suppose we want the third bit
(the leftmost bit is bit-1). The following declaration is valid:
BOOL bit3 = 3 ELEM bi
Thus, if the third bit is a 1
, the declaration will give the value
TRUE
for bit 3. In fact, 3
written in binary is 00112, so bit 3 is
1
. Thus
2 ELEM bi
would yield FALSE
. The priority of ELEM
is 7.
Incidentally, notice that in Algol 68 the most significant bit in a machine word is bit-1 and the least significant bit is bit-32. This strongly suggests that computers in the 1960's were “big-endian”. Intel microprocessors and other compatible processors are “little-endian”12.1. Because the a68toc compiler translates Algol 68 programs into C programs, it is quite possible for the Algol68toC system to be implemented on a “big-endian” microprocessor, such as the Motorola 68000-series. A “big-endian” processor stores a machine word as four bytes (each of 8-bits) with the most significant byte at the lowest memory address. “Little-endian” processors store the least significant byte at the lowest memory address. Whatever kind of microprocessor is used to elaborate your programs, the most significant bit of the word is bit-1 and the least significant bit is bit-32 in Algol 68.
The dyadic operators
SHL and SHR
shift a machine word to the left or to the right respectively by the
number of bits specified by their right operand. Their priority is
8
. To illustrate their action we shall suppose that they
all operate on the BITS
value 16r 89ab cdef
.
Both the shifts are by four bits which is equivalent to one
hexadecimal digit (4 bits is half a byte and is commonly called a
nibble: yes, software engineers do possess a sense
of humour!).
The result of shifting the above value by 4 bits is given by the following table:
Original value = 16r 89ab cdef | ||
Operator | Bits shifted | Yield |
SHL | 4 | 9abc def0 |
SHL | -4 | 089a bcde |
SHR | 4 | 089a bcde |
SHR | -4 | 9abc def0 |
When shifting left (SHL
), bits shifted beyond the most significant
part of the word are lost. New bits shifted in from the right are always zero.
When shifting right (SHR
), the reverse happens. Note that the
number of bits shifted should be in the range [- 32, +
32]. For SHL
, if the number of bits to be shifted is
negative, the BITS
value is shifted to the right and likewise for
SHR
. The header for SHL
is
OP SHL = (BITS b,INT i)BITS:
and correspondingly for SHR
. The value b
is the value to be shifted and the integer i
is the
number of bits to shift. UP
and DOWN
are
synonyms for SHL
and SHR
respectively. The
priorities of SHL
and SHR
are both 8.
As well as the operators =
and /=
(which have the
usual meaning), the operators <=
and >=
are
also defined for mode BITS
. The formula
s >= t
yields TRUE
only if for all bits in t
that are
1
, the corresponding bits in s
are also
1
. This is sometimes regarded as “s implies t”.
Contrariwise, the formula
s <= t
yields TRUE
only if for all bits in t
which are
0
, the corresponding bits in s
are also
0
. Likewise, this is sometimes regarded as “NOT
t
implies s
”.
BITS a = 16r 1111 1111, b = 16r 89ab cdefwhat is the value of each of the following: Ans
a AND b
a OR b
NOT a OR b
[Hint: convert each value to radix 2 and
then combine]
a = b
16rab SHL 3
16rba SHR 3