Lab Exercise 4: Operators

# Objective

The purpose of this lab is to illustrate the use of several arithmetic and logical operators of the C language. In this exercise we will work with the various forms of the assignment operator, the basic arithmetic operators, the increment and decrement operators, and the bit operators. Upon completion of this exercise, you should understand how to code basic arithmetic and logical expression statements.

## Software Tools

Installation
Instructions
Windows Linux Mac OSX
MPLAB® X
Integrated Development Environment
MPLAB® XC16
C Compiler

## Exercise Files

Installation
Instructions
Windows Linux Mac OSX
Project and Source Files

# Procedure

1

### Open the Project

Start MPLAB® X, then click on the Open Project icon on the main toolbar

Navigate to the folder where you saved the exercise files for this class.

Click on the Lab04.X folder.
Select Open Project .

2

### Open C Source File

Open the Lab04.c source file from the project tree by double clicking on its icon.

This will bring up Lab04.c in a window to edit

3

### Edit Source File

Search the code for lines with the comment “//### Your Code Here ###”. There will be additional comments above these lines with complete instructions, and in some cases there will be comments to the right with specific details regarding a particular line of code.
Note that comments with instructions for your tasks are surrounded by ‘#’ to make them easy to spot.

STEP 1:
Add charVariable1 to charVariable2 and store the result in charVariable1. Algebraically speaking, this is equivalent to x = x + y. There are two ways of accomplishing this task in C. On line 52, perform this operation using the + operator (similar to the algebraic syntax). On line 54, perform this same operation again, but this time using the += operator.

STEP 2:
Increment charVariable1. There are several ways this could be done. Use the one that requires the least amount of typing. Algebraically, this is equivalent to x = x + 1.

STEP 3:
Use the conditional operator to set longVariable1 equal to intVariable1 if charVariable1 is less than charVariable2. Otherwise, set longVariable1 equal to intVariable2. If we were to do this the long way, it might look something like this:

if (charVariable1 < charVariable2)
longVariable1 = intVariable1;
else
longVariable1 = intVariable2;

However, here we want to make use of the conditional operator ‘ ? : ‘ to perform the same task. Remember, the syntax of the conditional operator is:

```(test-expr) ? do-if-true : do-if-false;
```

STEP 4:
Shift longVariable2 one bit to the right. There are several ways this can be done, but the shortest is to use the appropriate compound assignment operator. Algebraically, this can be represented as: x = x / 2, but the shift operators in C will perform this operation much more efficiently than a divide operator could.

STEP 5:
Perform the logical operation (longVariable2 bitwise-AND 0x30) and store the result back in longVariable2. Once again, the fastest way to do this is to use the appropriate compound assignment operator that performs the equivalent operation to: longVariable2 = longVariable2 & 0x30. If you need additional hints, take a look at the code below this step in the source file.

4

### Debug Project

Once you finish writing the code:

Click on the Debug Project button. This will build and send the program to the simulator.
Click on the Continue button. This begins the simulation.
Click on the Halt button. This will stop execution so that we may examine the variables and their values.

Open the Variables Window with either Window -> Debugging -> Variables or ( Alt + Shift + 1)

# Results

5

### End Debug Session

End the Simulation Session by clicking the Finish Debugger Session button.

Close the Project.

# Conclusions

Hopefully this gives you a feel for how the various C operators work. There was a strong emphasis on the compound assignment operators because that is the one area most new C programmers have the most difficulty with, but that experienced C programmers use most often.

The program itself doesn't do anything particularly useful. Its intent was to provide a platform to learn and experiment with C’s operators without bogging you down with things not related to the topic at hand.