Operators


Operators are symbols, which are used for certain mathematical or logical manipulations. In C language there are 4 types of operators used to operate on data and variables: arithmetic, relational, logical and bitwise.

Arithmetic Operators


The most basic arithmetic operators are the operators of addition, subtraction, multiplication and division. However, depending on the type of data the same arithmetic operations may have different values. For example division of an integer variable x=5 by 2 will give a result of 2, whereas the same operation for the floating point number will result in precise 2.5 number. Another type of arithmetic operators is the “%” modulo operator, which gives the value of the remainder from the division operation. It is worth mentioning that the modulo operator should be applied to positive numbers only. Subsequent type of operators is the assignment operator. There are several ways to write programs and reduce the volume of code, using assignment operators is one of them.
Example:
x =x + y is exactly the same as x+=y

In the similar way the modulo, subtraction, multiplication and division operators can be used. The last but not least arithmetic operation to be mentioned is the incrementation and decrementation of the variable, which is declared as follows: ++x or --y.

Logical Operators


There are three main Logical operators used in C and they are used to compare or evaluate logical and relational expressions:
Logical OR “||”
Logical AND “&&”
Logical NOT “!”

Logical OR is used to combine 2 expressions or the condition evaluates to true if any one of the 2 expressions is true.
Example 1:
((32<5) || (34>5))
The expression is true, because the right side of the expression is true.

Example 2:
((32<5) || (43<6))
The expression is false, because none of the expressions is true.

Logical AND is used to evaluate 2 conditions or expressions with relational operators simultaneously. If both the expressions to the left and to the right side of the logical operator is true then the whole expression is true.

Example 1:
((32<5) && (34>5))
The expression is false, because the left side of the expression is false.

Example 2:
((32>5) && (43>6))
The expression is true, because both of the expressions are true.

Logical NOT operator takes a single expression and evaluates to true if the expression is false and evaluates to false if the expression is true.

Example 1:
!(x>=y)
The expression evaluates to true only if the value of x is neither greater than or equal to y.

Bitwise Operators


Bitwise operators only work on limited types: int and char. These operators are used when the access or a manipulation of individual bits is needed.
Bitwise operators:
Bitwise AND “&”
Bitwise OR “|”
Bitwise exclusive OR (XOR) “^”
Left shift “<<”
Right shift ”>>”
One’s complement “~”

Bitwise AND will copy a bit to the result if it exists in both operands.
Example:
0110 & 0011 ⇒ 0010
Bitwise OR will copy a bit if it exists in either operand.
Example:
0110 | 0011 ⇒ 0111
Bitwise XOR copies the bit if it is set in one operand (but not both).
Example:
0110 ^ 0011 ⇒ 0101
Left Shift shifts the bits of a number by a given number of positions specified on the right side of the operator. In the binary system shifting left by one position doubles the value of the number.
Example:
11<<1

11 = 00001011

11<<2 = 00101100

Right Shift shifts right the left operands value by the number of bits specified by the right operand. In the binary system shifting right by one position divides the value of the number by 2.
Example:
6>>1

6 = 00000110

6>>1 = 00000011
One’s complement requires one operand and has the effect of flipping the bits.
Example:
~0011 ⇒ 1100

Relational Operators


Relational operators are used to compare the relationship between operands and bring out a decision and program accordingly. There are the following relation operators available in C:
x < y less than
x <= y less than or equal to
x > y greater than
x >= y greater than or equal to
x == y is equal to
x != y is not equal to