Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Operators - An operator is a symbol that tells the compiler to perform specific mathematical or
logical manipulation.
Arithmetic Operators.
Assignment Operators.
Relational Operators.
Bitwise Operators.
Logical Operators.
Misc Operators.
Arithmetic Operators – There are five Arithmetic Operators supported by the C++ language.
e.g. : a=11%3;
the variable ‘a’ will contain the value 2,since 2 is the remainder from dividing 11 between 3.
e,g : a=5;
>= Checks if the value of left operand is greater (A>=B) is not true
than or equal to the value of right operand, if
yes then condition becomes true.
Bitwise Operator – Bitwise Operators modify variables considering the bit patterns that
represent the values they store.
Symbols Operators
| Bitwise inclusive OR
0 0 0
0 1 0
1 0 0
1 1 1
Bitwise OR “|”
Bit a Bit b a | b (a OR b)
0 0 0
0 1 1
1 0 1
1 1 1
0 0 0
0 1 1
1 0 1
1 1 0
0 1
1 0
Right shift (>>) – The symbol for right shift operator is>>. For its operation, it requires
two operands. It shifts in its left operand to the right. The number of places the bits are
shifted (i.e. the right operand). Thus by doing ch>>3 all the bits will be shifted towards
right by three places and so on.
Left shift (<<) – The symbol of left shift operator is <<. It shifts each bit in its hand
operated to the left by the number of positions indicated by the right -hand operand. It
works opposite to that of right shift operator. Thus by doing
ch << 1 in the above example we have 11001010. Blank spaces generated are filled up by zeroes
as above.
Logical operators : These operators are used to perform logical operations on the given
expressions. There are 3 logical operators in C language. They are, logical AND (&&),
logical OR(||) and Logical NOT (!).
NOT Operator (!) – This Operator is used to perform Boolean operation NOT, it has only
one operand, located at its right, and the only thing that it does is to inverse the value of
it, producing false if its operand is true and true if its operand is false. Basically, it
returns the opposite Boolean value of evaluating its operand.
Example (screenshot)
The logical operators && and || are used when evaluating two expressions to obtain a
single relational result.
&& OPERATOR : This operation results true if both its two operands are true, and false
otherwise.
A B A && B
|| OPERATOR : This operation results true if either one of its two operands is true, thus being
false only when both operands are false themselves.
A B A || B
Misc Operator
Sizeof - sizeof operator returns the size of a variable. e.g., sizeof(a), where ‘a’ is integer,
and will return 4.
Condition? X : Y – condition operator (?). if Condition is true then it returns value of X
otherwise returns value of Y.
Comma operator (,) – Comma Operator causes a sequences of operators to be
performed. The value of the entire comma expression is the value of the last expression
of the comma-separated list.
dot (.) and -> (arrow) – Member operators are used to reference individual members of
classes, structures, and unions.
Cast – casting operator converts one data typeto another data type. e.g., int(2.2000)
would return 2.
Pointer operator (&) – pointer Operator ‘&’returns the address of a variable. e.g., &a;
will give actual address of the variable.
Pointer operator (*) – pointer Operator ‘*’ is pointer to a variable. e,g.,*var; will pointer
to a variable var.
For example x = 7+3*2; here,x is assigned 3, not 20 because operator * has higher precedence
than +, so it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appearat the top of the table,those with the lowest
appear at the bottom. Within an expression, higher precedence operators will be evaluated first.