Lab Exercise 4: Operators

Last modified by Microchip on 2023/11/09 09:21


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.

Reference Materials

Exercise Files

Back to Top


Open the Project

Start MPLAB® X IDE, then click on the Open Project Main 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 Open Project Button

Open C Source File

Open the 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.

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.

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.

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.

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;
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;

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.

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.

Debug Project

Once you finish writing the code:

Click on the Debug Project Main Debug Project button. This will build and send the program to the simulator.
Click on the Continue Debug Continue button. This begins the simulation.
Click on the Halt Debug Pause 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)


Variable watch window with the results

The Variables window will show the results you should have after running the entire program.

Now, let’s take a look at the code and see how this all came about.

Note: longVariable2 is repeatedly incremented at the end of the program. Therefore, its value will not necessarily match the value in the screenshot at left.

Did the values for floatvariable1 and floatvariable2 print out a hexadecimal numbers (0x420A456D and 0x4091248D) instead of floating point format? If so, this is because the Value column is set up to display in Hexadecimal format by default. To change to float right click on the value you want to change then select “Display Value Colum As” , select “IEEE float”.

Code Analysis

(NOTE: Line numbers correspond to those in the provided solution file.)

Up near the top, we declared several variables and gave them initial values:

Lines 31-38
char charVariable1 = 50;
char charVariable2 = 100;
float floatVariable1 = 34.5678;
float floatVariable2 = 156.78956;
int intVariable1 = 1000;
int intVariable2 = 10000;
long longVariable1 = 1000;
long longVariable2 = 2000;

Lines 52-54

In the main() function, we perform a variety of arithmetic and logic operations on the variables. In step 1 of the source code, we were asked to perform two addition operations where one of the variables was both an operand as well as the target for the result. The two lines should have looked something like this:
charVariable1 = charVariable1 + charVariable2;
charVariable1 += charVariable2;

The first one is the long way of doing this. It is perfectly correct to do it this way, but most C programmers prefer the second method, which uses the compound assignment operator ‘+=‘. Aside from the stereotype that C programmers like to write the most cryptic code possible, this method does require less typing, and also forces the compiler to recognize that charVariable1 is both a source operand and the result destination of the operation. This can lead to more compact code with some compilers that don’t take the time to notice that the same variable is on both sides of the equals sign of the first method.

Line 61

In step 2 of the source file, we are asked to increment the variable charVariable1. There are at least three ways this can be done. The first two are similar to what we saw above:
charVariable1 = charVariable1 + 1;
charVariable1 += 1;

But the shortest way to do this is to use C’s increment operator ‘++’:
or, in this case we could also use the prefix version:
In all three cases, the result would be that charVariable1’s value would be incremented by one after the operation.

The next four lines of code illustrate the use of several other operators: subtraction delete, decrement (- -), multiply compound assignment (*=) and divide compound assignment (/=). If you are having trouble understanding any of these, try stepping through the code to see how the variables change at each step.

Line 84

In step 3 of the source file, we are asked to use the conditional operator to set longVariable1 to intVariable1 if charVariable1 is less than charVariable2 and to intVariable2 otherwise. As shown in the procedure section, this could be written out the long way as:

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

Using the conditional operator, this could be coded as:
longVariable1 = (charVariable1 < charVariable2) ? intVariable1 : intVariable2;

The expression on the right of the equals sign will be equal to intVariable1 if the expression before the question mark is true, and it will be equal to intVariable2 if the expression is false.

The next line of code illustrates an example of the comma operator. Honestly, this isn't a particularly common way of using it. When we get to the section on for loops, you will see one of its more common uses.

Line 101

In step 4 of the source file, we are asked to shift longVariable2 one bit to the right, which is the equivalent of dividing by two. As with other operations, there are several ways this could be accomplished:
longVariable2 = longVariable2 » 1;
longVariable2 »= 1;

The second method is the one we were going for here, but either one gets the job done.

Line 114

Finally, in step 5 of the source code file, we were asked to perform a bitwise AND between longVariable2 and the literal value 0x30 and store the result in longVariable2. Once again, there are a few ways of doing this:
longVariable2 = longVariable2 & 0x30
longVariable2 &= 0x30

As before, the second solution is the one we were looking for. The remaining code consists of a few more examples of various operators.

End Debug Session

End the Simulation Session by clicking the Finish Debugger Session Debug Finish Debugger Session button.Close the Project.

Back to Top


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.

Back to Top