Hardware Reference
In-Depth Information
Now, time to try that again with
RED
. The value of
CYAN
is 110, and
RED
is 001.
The i rst two bits are 1 in
CYAN
and 0 in
RED
. They return 0. The third bit is 0 in
CYAN
and 1 in
RED
. The logical
AND
process returns 000. There is no
RED
in
CYAN
because
CYAN
AND
RED
returns 0.
To read boolean data, read the byte containing the data from EEPROM and
then perform a logical
AND
with the reference value. To create boolean data, you
must take an empty variable (initialized as 0) and then perform logical
OR
opera-
tions with reference values. What happens if you want to update an existing
value? You already know how to set a bit, using a logical
OR
, but to clear a bit,
you must use a logical
NOT
AND
.
NOT
inverts a status; if it was previously
TRUE
,
it will become
FALSE
. By inverting the reference, you keep every bit that is set
except the one you want to clear. To toggle a bit, simply use the logical
XOR
to
invert its status.
XOR
, short for Exclusive
OR
, will be true if and only if one of the
inputs is
TRUE
; if they are both
TRUE
, then the result will be
FALSE
.
Figure 6-3 shows a table of logical operators, showing the effect of each.
A
B
A | B
A & B
A ^ B
~A
00
0
0
0
1
10
1
0
1
0
01
1
0
1
1
11
1
1
0
0
AND
Figure 6-3:
Logical operators
OR
XOR
NOT
Following is a short example of how to perform bitwise operations. A bitwise
OR
is performed using the
|
symbol:
value |= RED; // Bitwise OR. Sets the BLUE bit
To perform a bitwise
AND
, use the
&
symbol:
vavalue &= ~GREEN; // Bitwise AND. Clears the RED bit (AND NOT RED)
And i nally, to perform an exclusive
OR
, use the
^
symbol:
value ^= BLUE; // Bitwise XOR. Toggles the GREEN bit
Reading and Writing Strings
Strings are generally an array of char values and as such can be easily stored
and recalled. In Arduino, it's possible to use a char array as a string, or you
can use the String data type for more robust data manipulation, at the cost of
program size. With character arrays, you can recall the entire allocated memory
and print it out as required.
Search WWH ::
Custom Search