 Perform Simple Binary Arithmetic in C++ - dummies

# Perform Simple Binary Arithmetic in C++

A binary operator is one that has two arguments. If you can say var1 op var2, op must be a binary operator. The most common binary operators are the simple operations you performed in grade school. The binary operators are flagged in the table. (This table also includes the unary operators.)

Mathematical Operators in Order of Precedence
Precedence Operator What It Is
1 + (unary) Effectively does nothing
1 – (unary) Returns the negative of its argument
2 ++ (unary) Increment
2 — (unary) Decrement
3 * (binary) Multiplication
3 / (binary) Division
3 % (binary) Modulo
4 – (binary) Subtraction
5 =, *=,%=,+=,-= (special) Assignment types

Multiplication, division, modulus, addition, and subtraction are the operators used to perform arithmetic. In practice, they work just like the familiar arithmetic operations as well. For example, using the binary operator for division with a floating point double variable looks like this:

`double var = 133.0 / 10.0;`

The expression 133/10 performs integer division, producing the int result 13 rather than the floating-point 13.3.

Each of the binary operators has the conventional meaning that you studied in grammar school — with one exception. You may not have encountered modulus in your studies. The modulus operator (%) works much like division, except it produces the remainder after division instead of the quotient. For example, 4 goes into 14 three times with a remainder of 2. Thus we say 14 modulus 4 is 2:

`int var = 14 % 4; // var is set to 2`

Modulus is not defined for floating point variables.