Technologies

C

Conditional Operator or Ternary Operator in C Language
(adsbygoogle = window.adsbygoogle || []).push({});
# Conditional Operator :

# Operation of Conditional Operator :

** Examples : **

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

y = (a > b)? b : c;
# Bitwise Operator :

(adsbygoogle = window.adsbygoogle || []).push({});
(adsbygoogle = window.adsbygoogle || []).push({});

CLanguage

Friday, 31 Mar 2017

315

0

0

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

Syntax : exp1 ? exp2 ? exp3;

where exp1, exp2 and exp3 are expressions.

where exp1, exp2 and exp3 are expressions.

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

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.

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

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

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.

3. Greatest of three numbers using conditional operators.

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

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.

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 :

Highest

~

>>, <<

&

^

||

Lowest

~

>>, <<

&

^

||

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.

Examples 1 :

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.

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.

Examples 2 :

-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

-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

Post Article

Upload Youtube Video

CLanguage

Friday, 31 Mar 2017

315

0

0

Popular Post

- Write an algorithm and draw the flowchart to find the largest number among the three numbers?
- Write an algorithm an draw flowchart to find factorial of a number?
- Write an algorithm and draw the flowchart to find whether a given number is even or odd?
- Write an algorithm and draw the flowchart to Swap two integers?
- Write an algorithm an draw the flowchart to compute the average of the three numbers?
- Write an algorithm and draw the flowchart to find the largest number amoug two numbers?
- write a c program to swap two numbers
- How to write a algorithm?
- What is mean by Operator Precedence and Associativity in C Language?
- C Languages Relational Operators

All contents are copyright of their authors