# Conditional Operator :

It is also known as a **Ternary Operator** because it acts on three operands.

where exp1, exp2 and exp3 are expressions.

# Operation of Conditional Operator :

The value of exp1 is evaluated first. If it is true, value of exp2 is evaluated otherwise exp3 is evaluated.

**Examples :**

1. int a = 5, b = 10, c = 15;

y = (a > b)? b : c;

The expression a > b is evaluated, since it is false the value of c will be assigned to y. So the value of y will be 15.

y = (a < b)? b : c;

The expression a < b is evaluated, since it is true the value of b will be assigned to y. So, the value of y will be 10.

x = (a > b && a > c)? a : (b > c? b : c);

Here greatest of three numbers is calculated and stored in the variable x. If more than one conditional operators is there in an expression the **Conditional Operators** will be evaluated from right to left. **Conditional Operators **follow right to left associativity.

# Bitwise Operator :

C has distinction of supporting special operators known as bitwise operators for manipulation of data at bit level. These operators are used for testing the bits, or shifting them to right or left.

Operator |
Meaning |
---|---|

&& | Bitwise AND |

| | Bitwise OR |

^ | Bitwise Exclusive - OR |

<< | Shift left |

>> | Shift right |

~ | One's complement |

When bitwise operators are used with variables, they are internally converted to binary numbers and then bitwise operators are applied on individual bits. Bitwise operators perform manipulation on bits stored in memory. These operators work with char and int data types. They cnnot be used with floating point numbers.

The operators &, |, <<, >>, ^ are binary operators whereas the operator ~ is a unary operator.

Relative precedence of Bitwise operators is as follow :

~

>>, <<

&

^

||

Lowest

When equal priority operators are there in an expression, the operators will be evaluated from left to right i.e thy follow left to right associativity.

Bitwise NOT i.e one's complement (~) follows right to left associativity.

If a = 4, b = 6, find the values of all bitwise operators.

The equivalent binary value of a is 0 0 0 0 0 1 0 0

The equivalent binary value of b is 0 0 0 0 0 1 1 0

a. a & b is 0 0 0 0 0 1 0 0

The value of c = a & b is 4

b. a | b is 0 0 0 0 0 1 1 0

The value if c = a | b is 6

c. a ^ b is 0 0 0 0 0 0 1 0

The value of c = a^b is 2

d. c = a << 2. '<<' left shifts the binary bits of a by two positions.

c = 0 0 0 1 0 0 0 0

The value of c = a << b is 16.

e. c = a >> 2. '>>' right shifts the binary bits of a by two positions.

c = 0 0 0 0 0 0 0 1

The value of c = a >> b is 1.

f. c = ~a. The value of c is the 1's complement of a.

The value of c = ~a is -5.

-3 & 4 = 4 5 & -8 = 0 -15 & -29 = -31

-7 | 4 = -3 12 |-6 = -2 -24|-9 = -1

-5^6 = -3 9^-14 = -5 -4^-9 = 11

-15<<6=-960, -23<<4 = -368, -14>>9=1-, -5>>1=-3

~(-18) = 17, ~7=-8, ~(-3)=2, ~(-27)=26

## 0 Komentar untuk "Conditional Operator or Ternary Operator in C Language"