# LOGIC OPERATIONS IN 8051 C

SECTION 7.3: LOGIC OPERATIONS IN 8051 C

One of the most important and powerful features of the C language is its ability to perform bit manipulation. This section describes the action of bit-wise logic operators and provides some examples of how they are used.

Bit-wise operators in C
While every C programmer is familiar with the logical operators AND (&&), OR (||), and NOT (!), many C programmers are less familiar with the bitwise operators AND (&), OR (|), EX-OR (A), Inverter (~), Shift Right (»), and Shift Left («). These bit-wise operators are widely used in software engineering for embedded systems and control; consequently, understanding and mastery of them are critical in microprocessor-based system design and interfacing. See Table 7-3.
Table 7-3: Bit-wise Logic Operators for C

Examples 7-19 and 7-20 show the usage bit-wise operators.
Example 7-19
Run the following program on your simulator and examine the results.
Solution:
#include <reg51.h> void main (void)
{
P0 = 0×35 & OxOF; //ANDing
Pl = 0×04 | 0×68; //ORing
P2= 0×54 * 0×78; //XORing
P0= -0×55; //inversing
Pl= Ox9A » 3; //shifting right 3 times
P2= 0×77 » 4; //shifting right 4 times
p0= 0×6 « 4; //shifting left 4 times

Example 7-20
Write an 8051 C program to toggle all the bits of PO and P2 continuously with a 250 ms delay. Use the inverting operator.
Solution:
The program below is tested for the DS89C420 with XTAL = 11.0592 MHz.

Bit-wise shift operation in C
There are two bit-wise shift operators in C: (1) shift right ( »), and (2) shift left («).
Their format in C is as follows:
data » number of bits to be shifted right
data « number of bits to be shifted left

Example 7-21
Write an 8051 C program to toggle all the bits of PO, PI, and P2 continuously with a 250 ms delay. Use the Ex-OR operator.
Solution:

Example 7-22
Write an 8051 C program to get bit Pl.O and send it to P2.7 after inverting it. Solution:

Example 7-23
Write an 8051 C program to read the P1.0 and P1.1 bits and issue an ASCII character to PO according to the following table.

Next post:

Previous post: