Operators in C++ - ProgrammerTech

Search

# Operators in C++

## The concept of operators in C ++

Operators are symbols and each symbol has a specific meaning. The operators can be divided into five basic groups:

1. Arithmetic Operators.
2. Comparison Operators.
3. Logical Operators.
4. Bitwise Operators.
5. Assignment Operators.

## Factors used in arithmetic operations

1. The addition operator we use a sign ( + ).
2. The subtraction operator we use a sign ( - ).
3. The division operator we use a sign ( / ).
4. The remainder operator we use the sign ( % ).
5. The multiplication operator we use a sign ( * ).
6. The factor of increment by 1 we use a sign ( ++ ).
7. The decrease factor by 1 we use a sign ( -- ).

Example 1

``````#include <iostream>
using namespace std;
int main()
{
cout << 10 + 5 << endl;
cout << 10 - 5 << endl;
cout << 10 / 5 << endl;
cout << 10 * 5 << endl;
cout << 10 % 5 << endl;
return 0;
}``````

And the result of the previous example will be:

``````15
5
2
50
0``````

## Arithmetic Priorities in C++

• The increase and decrease when it comes before the number.
• braces.
• negative sign.
• Division and remainder of division and multiplication.
• equality.
• Late increase and decrease after number.

If there are two processes with the same priority, we start with the one closest to the left.

## Comparison Operators

1. A sign (>) is greater.
2. A sign ( < ) for the smallest.
3. Sign ( => ) is greater or equal to.
4. The sign ( =< ) is less than or equal to.
5. Signal ( == ) equals.
6. The sign ( =! ) is not equal.

## Logical Operators

• The result of any Boolean operation in CPLUS Plus is 0 or 1.
• The number 1 is true and the number 0 is false.
• Most of the use of logical tools is in conditional statements if we want to check two conditions or one of them.

### Boolean operation and in C++

The and operation is denoted by the symbol (&&), which is the intersection operator in C++, and the truth table of the and operation in C++ Plus is:

• 1 && 0 = 0.
• 0 && 1 = 0.
• 0 && 0 = 0.
• 1 && 1 = 1.

Example 2

``sum = 1 && 0;``

In this case the program will do an and between 0 and 1 and the output will be 0.

### Boolean operation or in C++

The or operation is denoted by the symbol (||), which is the effect of the meeting, and the truth table for the or operation in C++ Plus is:

• 1 || 0 = 1.
• 0 || 1 = 1.
• 0 || 0 = 0.
• 1 || 1 = 1.

Example 3

``sum = 1 || 0;``

In this example, the program will do an or between 0 and 1 and the output will be 1.

### Boolean not in C++

The not operation is denoted by the symbol (!), which is the negation operator, and the truth table of the not operation in C++ Plus is:

• 0 = 1.
• 1 = 0.

Example 4

``sum = 3!;``

Any value other than zero 0 is 1 means true the program will find the negation of 1 means 0 and the result is false.

## Bitwise Operators

The C++ language is characterized by the fact that it uses precise tools Bitwise Operators to deal with bits and is used with variables char, int and is not used with others and these tools are:

• The sign (~) means not negative.
• The sign (<<) means left shift.
• The pointer (>>) means right shift.
• The sign (^) means Xor is exceptional.
• The sign (&) means and multiply.
• The indicative (|) means or choose.

The priorities are in the order of the table.

### The negation operator ~ in Bitwise

The validation table in the negation operator ~ is as follows.

• ~true = false.
• ~false = true.

A question

If we have x=26 and its opposite in binary (00011010), what is the negative value of x, meaning x~?

### Multiplication operator & in Bitwise

The verification table is as follows

• true & true = true.
• true & false = false.
• false & false = false.

A question

If we have x=26 and y=9 convert the values ​​from decimal to binary and find the values ​​x&y?

Answer: We write the values ​​in 8-bit squares as follows:

In the cells in the first line we have converted the x-value of the binary and the cells in the second line we have converted the y-value of the binary. And in the third line, we solve the question according to the verification table that we wrote previously for & and he gave us the answer in binary 00001000, which means the number 8.

### selection factor | in Bitwise

The verification schedule is as follows:

• true | true = true
• true | false = true
• false | false = false

A question

If we have x=26 and y=9 convert the values ​​from decimal to binary and find the value of x|y?

Answer: We write the values ​​within 8-bit squares as before:

In the cells in the first line we have converted the value of x to binary and the cells in the second line we have converted the y value to the binary. In the third line, we solve the example according to the verification table that we wrote earlier for | And he gave us the answer in binary 00011011, which means the number 27.

### Bump factor ^ at Bitwise

The verification table is as follows

• true ^ true = false
• true ^ false = true
• false ^ false = false 