A.4. FUNDAMENTAL PROGRAMMING CONCEPTS 375
For example, in the following code segment the value of the variable “i” is incremented. This
is a shorthand method of executing the operation “i = i + 1;”
unsigned int i;
Example: It is not uncommon in embedded system design projects to have every pin on a microcon-
troller employed. Furthermore, it is not uncommon to have multiple inputs and outputs assigned to
the same port but on different port input/output pins. Some compilers support speciﬁc pin reference.
Another technique that is not compiler speciﬁc is bit twiddling. Figure A.7 provides bit twiddling
examples on how individual bits may be manipulated without affecting other bits using bitwise and
unary operators [ImageCraft].
a | b
a & b
a ^ b
bitwise exclusive or
P2OUT |= 0x80; // turn on bit 7 (msb)
if ((P2IN & 0x81) == 0) // check bit 7 and bit 0
P2OUT ^= 0x80; // flip bit 7
P2OUT &= ~0x80; // turn off bit 7
Figure A.7: Bit twiddling [ImageCraft].
A.4.2 PROGRAMMING CONSTRUCTS
In this section, we discuss several methods of looping through a piece of code. We will examine the
“for” and the “while” looping constructs.
The for loop provides a mechanism for looping through the same portion of code a ﬁxed
number of times. The for loop consists of three main parts:
• loop initiation,
• loop termination testing, and
• the loop increment.