Introduction to Operators in C/C++

Tutorial of Various Programming Mathematical and Logical Operations

© Guy Lecky-Thompson

Introduction tutorial for C and C++ programmers detailing how the various unary and binary operators work, and the effects that using them can bring.

Introduction

This tutorial article is an introduction to unary and binary operators. A binary operator is one that has a parameter on either side, while a unary operator has only one parameter. After reading this article, the reader should understand the usage of operators in C and C++ programming well enough to decipher some of the obfuscated code that is often found in third party projects that they might inherit.

Operators

The principle operators in C based languages are:

The first five basic operators need very little introduction : they are the standard mathematical operators for addition, subtraction, division and multiplication, along with the traditional equals sign used to assign the result of the calculation to a variable. In other words:

int nInteger = 3 + 1;

However, the following would not work:

int nInteger;
3 - nInteger = 1; // invalid code

The last two operators on the list are left shift and right shift. They allow the programmer to actually move the bits in a byte one way or the other. Therefore, if we were to have a number 64, encoded in binary, we could write it thus:

0 1 0 0 0 0 0 0

If we were to shift the bits to the left (64 << 1), we would end up with 128:

1 0 0 0 0 0 0 0

If we were to shift the bits to the right (64 >> 1), we would end up with 32:

0 0 1 0 0 0 0 0

So, these operators, besides anything else, give us quick access to fast divisions by powers of two, something which can be very useful in programming, especially when combined with Boolean logic.

Boolean Logic Operators

The two principle Boolean logic operators provided in C are:

These are the traditional AND and OR Boolean operations. Taking our example from above, if we were to AND the 64 and 128 values together, we would end up with 0:

1 0 0 0 0 0 0 0
AND 0 1 0 0 0 0 0 0
= 0 0 0 0 0 0 0 0

However, were we to OR these two values together, we would end up with 192:

1 0 0 0 0 0 0 0
OR 0 1 0 0 0 0 0 0
= 1 1 0 0 0 0 0 0

The other common Boolean operator is XOR (eXclusive OR), represented by:

This works as a filter, where a combination of two 1 values will yield 0, but when either input is 1 it will yield 1. So, 192 ^ 128 will yield 64:

1 1 0 0 0 0 0 0
XOR 1 0 0 0 0 0 0 0
= 0 1 0 0 0 0 0 0

Boolean logic operators are binary - they need a parameter each side, like any other mathematical operator. However, binary operators can also be used in a unary fashion.

Pre- and Post Increment/Decrement

Some common uses for the binary operators can be found in simple C programming statements such as for loops:

for (int i = 0; i < 100; i++)

We have placed the post increment operator ++ in bold for ease of identification. Essentially, this piece of code tells the computer to add one to i at each iteration. This is using + as a post increment. That is, the value i is evaluated first, and then one is added to it. So, we could write code such as:

int nX, nY, nZ;
nX = 1;
nY = 2;
nZ = nX + nY++;

After execution, nX contains 1, nY contains 3, and nZ contains 3. However, we can also write:

nZ = nX + ++nY;

After executing this code, nX contains 1, nY contains 3, but nZ now contains 4. This is because the value of nY has been pre-incremented; one has been added to it before the variable has been evaluated as part of the expression. The - operator can be used in a similar fashion, as a pre or post decrement operator. The other mathematical or Boolean operators can not be used in this way, however, they can be combined with the = operator to achieve some similar behavior.

The = Operator

We can use any of the mathematical operators with the = sign. For example, the following two statements are equivalent:

These will both compile happily and produce the same result : incrementing nX. However, we can also write:

nX += 2; // add 2
nX /= 10; // divide by 10
nX *= nX; // square
nX ^= 128; // XOR with 128

While these can be useful, they should be used with care, as it is easy to lose sight of the original intention of the code, especially if one encounters code such as:

nX /= ++nY + nZ++;

This level of obfuscation is all too common in programming where shorthand elegance is often preferred over clarity. However, a compiler may produce more optimized code for the above single line, than for the longhand equivalent:

nY = nY + 1;
nX = nX / (nY + nZ);
nZ = nZ + 1;

With this short guide in hand, most examples should be easily decoded, and possibly even rewritten in cases where clarity is more important than efficiency.


The copyright of the article Introduction to Operators in C/C++ in Computer Programming Tutorials is owned by Guy Lecky-Thompson. Permission to republish Introduction to Operators in C/C++ must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo