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

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

Exercise Files

File Download
Installation
Instructions
Windows Linux Mac OSX
Project and Source Files

 Procedure

1

Open the Project

Start MPLAB® X, then click on the Open Project Main_Open_Project.png 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 OpenProjectButton.png.

2

Open C Source File

Lab04C.png

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 Main_Debug_Project.png button. This will build and send the program to the simulator.
Click on the Continue Debug_Continue.png button. This begins the simulation.
Click on the Halt Debug_Pause.png 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 Debug_Finish_Debugger_Session.png 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.

20th Annual
Microchip MASTERs Conference 2016
Register now - Deadline: July 29

JW Marriott Desert Ridge Resort-Phoenix, AZ

© 2016 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.