Commodore 128 Programmer's Reference Manual page 108

Hide thumbs Also See for 128:
Table of Contents

Advertisement

98
COMMODORE 128
5=
0000
0101
6=
0000
0110
u
ORing
7=
0000
0111
The result 0111 is decimal 7. Notice from the right that 1 OR 0= 1, 0 OR 1 = 1, 1 OR
1 = 1 and 0 OR 0 = 0.
Understanding how these OR and AND combinations work gives you the power to
^
control individual bits within your computer's memory. Many of the 8-bit bytes utilize
each bit for separate control parameters.
\ ,
USING OR AND AND TO MODIFY
"
THE BIT VALUES IN A BYTE
A byte is a group of eight binary digits labeled, from right to left, 0 to 7. Each binary
| I
digit position represents a decimal value equal to two raised to the power of the position
^J
number. For example, the decimal value of position 6 is 2**6 or 64. From left to right
the positions are:
i
i
i J
76543210
^
and the corresponding values in decimal are:
128
64
32
16
8
4
2
1
To turn on a bit, place a "1" in its position. To turn it off, enter a "0". Hence the
binary 10010000 has bits 4 and 7 on. Their values are 128 and 16. So if a particular byte
is POKED with 144 (128 + 16), these two bits are turned on. To turn bits on, store
(POKE) a new value to that byte—a value equal to the sum of all the decimal
equivalents of all the bits that are enabled (on). Of course, you do not always know
which bits are already on. You may only want to turn on specific bits without
affecting the others. That's the purpose of the logical operations AND and OR.
First, obtain the decimal value of the byte by PEEKing. Then add the decimal
value of the bit you wish to turn on. The following command turns on bit 2 of memory
address "V":
POKEV, PEEK(V) + 4
This assumes bit 2 (third bit from the right) had a value of 0. Had it already been
"on," it would have no effect. To prevent such confusion, the C128 uses the power of
Boolean Logic.
Ideally you want to read (PEEK) each bit. The proper approach is to OR the byte
with an operand byte which will yield the desired binary value. Suppose we want to
turn on bit 5; the operand byte becomes 00100000. By ORing this with any byte it will
affect only bit 5, because any combination involving 1 in an OR operation results in 1.
Thus no bit already ON can be inadvertently turned off.
POKEV,PEEK(V) OR 32
Just as OR turns a switch on, AND can turn a switch off—with a slight difference.
AND results in a "1" only if both bits compared are "1." The trick is to compare the

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents