Previous | Home | Next |

## Hierarchy of operators

There are some operators which are given bellow with their mean. The higher the position of an operator is, higher is its priority.

Operator | Type |

! | Logical NOT |

*/ % | Arithmetic and modulus |

+ - | Arithmetic |

<> <=>= | Relational |

==!= | Relational |

+ - | Arithmetic |

<> <=>= | Relational |

|| | Logical OR |

= | Assignment |

## Associativity of operator

When an expression contains two operators of equal priority the tie between them is settled using the associatively of the operators.

Associatively can be of two types—Left to Right or Right to Left.

Left to Right associatively means that the left operand must be unambiguous. Unambiguous in what sense? It must not be involved in evaluation of any other sub-expression. Similarly, in case of Right to Left associatively the right operand must be unambiguous. Let us understand this with an example.

Consider the expression

a = 3 / 2 * 5 ;

Here there is a tie between operators of same priority, that is between / and *. This tie is settled using the associatively of / and *. But both enjoy Left to Right associatively.

### Logical operators

There are only three logical operator in C.

Operator | Type |

&& | AND |

|| | OR |

! | NOT |

The first two operator allows two or more conditions to be combined in an if statement.

The third logical operator is !(NOT) operator. This operator reverses the result of the expression evaluates to a non-zero value, then applying ! operator to it results into a 0.

## Use of logical operators

C allows usage of three logical operators, namely, &&, || and !.These are to be read as 'AND' 'OR' and 'NOT' respectively. here we can take an example to under stand the logical operators which is given below:

main( ) { int m1, m2, m3, m4, m5, per ; printf ( "Enter marks in five subjects " ) ; scanf ( "%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5 ) ; per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ; if ( per >= 60 ) printf ( "First division" ) ; if ( ( per >= 50 ) && ( per < 60 ) ) printf ( "Second division" ) ; if ( ( per >= 40 ) && ( per < 50 ) ) printf ( "Third division" ) ; if ( per < 40 ) printf ( "Fail" ) ; }

As can be seen from the second if statement, the && operator is used to combine two conditions. 'Second division' gets printed if both the conditions evaluate to true.

If one of the conditions evaluate to false then the whole thing is treated as false.

By the above example we can understand the else if condition which is only the arrangement of else with if.

## Bitwise operator

One of C's powerful features is a set of bit manipulation operators. These permit the programmer to access and manipulate individual bits within a piece of data. The various Bitwise Operators available in C are shown below:

Operator | Meaning |

~ | one's compliment |

>> | right shift |

<< | left shift |

& | bitwise AND |

| | bitwise OR |

^ | bitwise XOR (exlucive OR) |

These operators can operate upon ints and chars but not on floats and doubles. Before moving on to the details of the operators.

## one's compliment operator

On taking one's complement of a number, all 1's present in the number are changed to 0's and all 0's are changed to 1's. For example one's complement of 1010 is 0101. Similarly, one's complement of 1111 is 0000.

## right shift operator

The right shift operator is represented by >>. It needs two operands. It shifts each bit in its left operand to the right. The number of places the bits are shifted depends on the number following the operator (i.e. its right operand).

For example, if the variable ch contains the bit pattern 11010111, then, ch >> 1 would give 01101011 and ch >> 2 would give 00110101.

## left shift operator

This is similar to the right shift operator, the only difference being that the bits are shifted to the left, and for each bit shifted, a 0 is added to the right of the number.

## bitwise AND operator

This operator is represented as &. Remember it is different than &&, the logical AND operator. The & operator operates on two operands. While operating upon these two operands they are compared on a bit-by-bit basis. Hence both the operands must be of the same type (either char or int). The second operand is often called an AND mask. The & operator operates on a pair of bits to yield a resultant bit. we can make a truth table for this operator which is given below:

& | 0 | 1 |

0 | 0 | 1 |

1 | 0 | 1 |

bitwise OR operator- Another important bitwise operator is the OR operator which is represented as |. The rules that govern the value of the resulting bit obtained after ORing of two bits is shown in the truth table below:

| | 0 | 1 |

1 | 0 | 1 |

1 | 1 | 0 |

Using the Truth table confirm the result obtained on ORing the two operands as shown below. 11010000 Original bit pattern

00000111 OR mask

-------------

11010111 Resulting bit pattern

Bitwise OR operator is usually used to put ON a particular bit in a number.

## bitwise XOR operator

The XOR operator is represented as ^ and is also called an Exclusive OR Operator. The OR operator returns 1, when any one of the two bits or both the bits are 1, whereas XOR returns 1 only if one of the two bits is 1. The truth table for the XOR operator is given below:

^ | 0 | 1 |

0 | 0 | 1 |

1 | 1 | 0 |

XOR operator is used to toggle a bit ON or OFF. A number XORed with another number twice gives the original number.

## Conditional operators

The conditional operators ? and : are sometimes called ternary operators since they take three arguments. In fact, they form a kind of foreshortened if-then-else. Their general form is

expression expression 2 : expression 3

the mean of the above form is ''if expression1 is true then the value returned will be expression2 otherwise the value returned will be expression3.

The following points may be noted about the conditional operators:

- It's not necessary that the conditional operators should be used only in arithmetic statements. This is illustrated in the following examples:
int i ; scanf ( "%d", &i ) ; ( i == 1 ? printf ( "Amit" ) : printf ( "All and sundry" ) ) ;

- The conditional operators can be nested as shown below:
int big, a, b, c ; big = ( a > b ? ( a > c ? 3: 4 ) : ( b > c ? 6: 8 ) ) ;

- Check out the following conditional expression:
a > b ? g = a : g = b ;

This will give you an error ‘Lvalue Required’. The error can be overcome by enclosing the statement in the : part within a pair of parenthesis. This is shown below:

a > b ? g = a : ( g = b ) ;

In absence of parentheses the compiler believes that b is being assigned to the result of the expression to the left of second =. Hence it reports an error.

The limitation of the conditional operators is that after the ? or after the : only one C statement can occur. In practice rarely is this the requirement. Therefore, in serious C programming conditional operators aren’t as frequently used as the if-else.

Previous | Home | Next |