Hardware Reference
In-Depth Information
The & operator is often used to clear one or more bits to 0. For example, the statement
PORTC 5 PORTC & 0xAA; /* PORTC is 8 bits */
clears the even bits of PORTC to 0.
The | operator is often used to set one or more bits to 1. For example, the statement
PORTB
5 PORTB Z 0xAA;
/* PORTB is 8 bits */
sets the odd bits of PORTB to 1.
The XOR operator can be used to toggle one or multiple bits. For example, the statement
abc 5 abc ^ 0xF0; /* abc is of type char */
toggles the upper 4 bits of the variable abc .
The .. operator shifts the involved operand to the right for the specified number of places.
For example, the statement
xyz 5 abc .. 3;
shifts the variable abc to the right three places and assigns it to the variable xyz .
The ,, operator shifts the involved operand to the left for the specified number of places.
For example, the statement
xyz 5 xyz ,, 4;
shifts the variable xyz to the left four places.
The assignment operator 5 is often combined with the operator. For example, the
statement
PTP 5 PTP & 0xBD;
can be rewritten as
PTP & 5 0xBD;
and the statement
PORTB 5 PORTB k 0x40;
can be rewritten as
PORTB k 5 0x40;
5.3.6 Relational and Logical Operators
Relational operators are used in expressions to compare the values of two operands. If the
result of the comparison is true, then the value of the expression is 1. Otherwise, the value of
the expression is 0. Here are the relational and logical operators.
55 equal to (two “5” characters)
!5 not equal to
. greater than
.5 greater than or equal to
, less than
,5 less than or equal to
&& and
i or
! not (one's complement)
Here are some examples of relational and logical operators.
if (!(ADCTL & 0x80))
statement 1 ;
// if bit 7 is 0, then execute statement 1
 
Search WWH ::




Custom Search