Computer Concept & Programming In C - Unit II - 10

Q.11 Give precedence and associativity of operators.                       (AKTU. 2012 - 13)
Related Questions -
Q. What is the use of bitwise operators? Describe any two bitwise operators with example.                                                                                     (AKTU. 2008-09)
Or. What are the different bit operators used in C? Give an example of each.                                                                                          (AKTU. 2009-10)
(Look bitwise operators)
Ans. Operator Precedence And Associativity: -
Precedence Of Relational And Logical Operators: -
Table below shows the relative precedence of the relational and logical operators.
Operator Associativity Precedence
Arithmetic Operator
! Right to left Highest
> > = < < = Left to right
= = ! = Left to right
& &         Left to right
| | Left to right Lowest 
In order to see how the precedence works, look at the expression (a = =  b & & x = = y || m = = n). This expression is evaluated as TRUE if one of the following conditions is met:
1. If a = = b is evaluated as TRUE AND x = = y is evaluated as TRUE, OR m = = n is evaluated as TRUE.
2. This means that the operator && is evaluated before the operator | | because of having higher precedence. In other words, this expression is evaluated as if it had parentheses as follows: (a = = b && x = = y) | | (m = = n).
* Bit operators: -
Using C, you can have access to every bit in memory thus performing very low-level operations.
To understand clearly the working of bit wise operators let us take the following example:
You know that the number 8 is represented in binary as 00001000 and 7 as 00000111 respectively.
1. The bit operation & is known as bitwise AND.
It works as: 8&7 = 
  8 & 7 = 0
2. The bit operation 1 is known as bitwise OR.
8 | 7 = (00001000) | (00000111) = (00001111) = 15
8 | 7 = 15
3. The bit operation ~ is known as 1’s compliment. It is unary 
  ~ 8 = 247 or 255 – 8 = 247.
4. The bit operation >> is known as bit wise right shift
     
\ 8 >> 1 = 4
     
\ 8 >> 2 = 2
5. The bit operation << is known as bit wise left shift.
   
 \ 2 << 1 = 4

   
\ 2 << 2 = 8
Precedence of bit wise operators: -
Bit wise operators Associativity Precedence
~ From left to right Highest
<<   >> From left to right
& From left to right
^ (XOR) From left to right
| From left to right Lowest
6. The bit operation ^ is known as bitwise XOR
8 ^ 7 = (00001000) ^ (00000111) = (00001111) = 15 
\ 8 ^ 7 = 15
Finally the table below shows the precedence and associativity of all the C operators.
Remember that when two operators have the same precedence they are evaluated according to their associativity.
Precedence of C operators
Operator            Associativity Precedence
( ) [ ] ->              Left to right Highest
! ++ - -
& * (typecast) size of      Right to left
* / %              Left to right
+ -                      Left to right
<< >>              Left to right
< < = > > =              Left to right
= = ! =             Left to right
&              Left to right
^             Left to right
|              Left to right
&&                      Left to right
| |             Left to right
?:            Left to right
= + = - = *= /=    Right to left
,            Left to right            Lowest