![]() |
Tutorials > C++ > Arithmetic Operators
IntroductionIt is almost impossible to create a program that has any use without needing to use some sort of arithmetic. A great deal of mathematics is involved with programming. This tutorial will explain how to accomplish basic arithmetic in C / C++. Contents of main.cpp : #include <iostream> #include <stdlib.h> using namespace std; int main() { We will be using integers for this tutorial. Many other data types such as floats and doubles may also be used. Even a char has a numerical value known as the ascii value. // Variable Declarations //----------------------- int x = 10; int y = 5; int z = 0; The basic arithmetic operators can be shown in the following table :
An arithmetic expression can consist of static values as well as variables. You can
imagine that the result of the expression replaces it eg. // Add, Subtract, // Multiply & Divide //------------------- z = x + y; cout << "10 + 5 = " << z << endl; z = x - y; cout << "10 - 5 = " << z << endl; z = x * y; cout << "10 + 5 = " << z << endl; z = x / y; cout << "10 / 5 = " << z << endl;
As the var = var {operator} {expression} is used
quite often, other operators are available. Remember that a single number
is also an expression in itself. These operators include
z = 5;
z += 5;
cout << "5 + 5 = " << z << endl;
Other operator effects are more easily seen be looking at the binary representation of a number. If you do not understand why 22 is equal to 10110, please e-mail me and I'll put up a tutorial on binary numbers. If I do not receive an e-mail, I'll assume that you know how to represent decimal numbers as binary numbers. x = 22; // 10110 y = 17; // 10001
Just before we delve into the binary part, another operator MOD can be used.
This operator is used by placing a % character. For those
who do not know what the MOD operator is, it is essentially the
remainder of a division expression. // Mod //----- cout << "10 MOD 5 = " << 10 % 5 << endl; cout << "11 MOD 5 = " << 11 % 5 << endl;
I am going to briefly explain the binary operators. Once again, if you are not happy
with it, you can e-mail me and I will put up a separate tutorial on binary arithmetic.
0 & 0 = 0 Therefore :
// Binary AND, OR and XOR //------------------------ cout << "22 & 17 = " << (x & y) << endl;
The | operator is known as the Bitwise OR operator.
The result is 0 only if both operands are 0.
0 | 0 = 0 Therefore :
cout << "22 | 17 = " << (x | y) << endl;
The ^ operator is known as the Bitwise XOR operator.
The result is 1 only if the operands are different.
0 ^ 0 = 0 Therefore :
cout << "22 ^ 17 = " << (x ^ y) << endl;
Other operators that we will discuss are the increment (++) and decrement (--)
operators. // Increment Operator //-------------------- cout << "22 + 1 = " << ++x << endl; cout << "x = " << x++ << endl; cout << "x + 1 = " << x << endl;
The next code segment shows the power of operator precedence. The 30 / 20
is calculated before the addition of the 5. If you wanted to add 5 to 30 first, you would
need to rewrite the line as :
z = (5 + 30 / 20);
cout << "5 + 30 / 20 = " << z << endl;
You may have noticed from the above code segment that the 30 / 20
produces 1 and not 1.5 as you
may suspect. This is because you are operating on 2 integers.
cout << "5 + 30 / 20.0 = " << (5 + 30 / 20.0) << endl; system("pause"); return 0; } You should now be able to accomplish basic Arithmetic in C / C++. I hope you found the tutorial useful as it took quite a while to put it together. Do not forget about how important brackets are and how operator precedence works. Also remember that the increment and decrement operators can be placed both before and after the variable. Please let me know of any comments you may have : Contact Me
All Rights Reserved, © Zeus Communication, Multimedia & Development 2004-2005 Read the Disclaimer |
|