0% found this document useful (0 votes)
7 views21 pages

1 Operators

The document provides an overview of various types of operators in programming, including arithmetic, relational, logical, bitwise, assignment, and miscellaneous operators. It explains their functions, usage, and provides examples for clarity. Additionally, it covers concepts like unary operations, conditional expressions, and bitwise operations with detailed examples.

Uploaded by

720724208015
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views21 pages

1 Operators

The document provides an overview of various types of operators in programming, including arithmetic, relational, logical, bitwise, assignment, and miscellaneous operators. It explains their functions, usage, and provides examples for clarity. Additionally, it covers concepts like unary operations, conditional expressions, and bitwise operations with detailed examples.

Uploaded by

720724208015
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

OPERATORS

OPERATORS
A symbol that denotes an operation that can be performed on some data.
• Arithmetic Binary
• Arithmetic Unary
• Relational
• Logical
• Bitwise
• Conditional
• Assignment
ARITHMETIC BINARY
Assuming int a=10,b=20;
ARITHMETIC UNARY
1) Sign Operator: +, -
Example: +5, -5
2) Increment and Decrement by 1
++, --
a)Pre-increment: y=++x; (x=x+1; and y=x;)
b)Post-increment: y=x++;  (y=x; and x=x+1;)
RELATIONAL OPERATORS
1) Operators used to compare two operands (variables, constants or
expressions)
2) Characters can be compared as well
3) These operators are used extensively with flow control statements.
4) Example: a!=b, a<=b
RELATIONAL OPERATORS
Operator What it does Example

== Equal to 5==5 will be 1

> Greater than 5>6 will be 0

< Less than 6<7 will be 1

>= Greater than equal to 2 >= 1 will be 1

<= Less than equal to 1 <= 2 will be 1

!= Not equal to 5 != 6 will be 1


ASSIGNMENT OPERATORS

Operator Example

= a=b or b=a

+= a += b or a = a+b

-= a -=b or a = a-b

*= a *= b or a = a*b

/= a /= b or a = a/b

%= a %= b or a = a%b
LOGICAL OPERATORS
In the C programming language, Logical operators are mostly used for decision
making. A logical operator returns either 0 or 1 whether the condition is true or
false.

Operator What it does


True only if all
&& (Logical AND)
conditions satisfy.
True only if either
|| (Logical OR) one condition
satisfies.
True only if the
! (Logical Not)
operand is 0.
LOGICAL OPERATORS: &&
• To be used when a statement or set of statements are to be executed
based on whether two expressions are true
• The expression on the RHS will be evaluated only if the expression on
the LHS is true.
Example: (x>10)&&(y<a)
• Also called “Short circuit and operator”
LOGICAL OPERATORS: ||

• To be used when a statement or set of statements are to be executed


based on whether at least any one of two expressions are true
• The expression on the RHS will be evaluated only if the expression on
the LHS is false.
Example: (x>10)||(y<a)
• Also called “Short circuit or operator”
Logical not: !
• Logical NOT is denoted by exclamatory
characters (!), it is used to check the opposite
result of any given test condition.
If any condition's result is non-zero (true), it
returns 0 (false) and if any condition's result is
0(false) it returns 1 (true).

Example:
num=10
!(num==10)  0
!(num!=10) 1
!(num>5) 0
!(num<5) 1
Bitwise AND operator - &

The output of bitwise AND is 1 if the corresponding bits of two


operands is 1. If either bit of an operand is 0, the result of
corresponding bit is evaluated to 0.
• EXAMPLE:
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bit Operation of 12 and 25
00001100
& 00011001
00001000 = 8 (In decimal)
Bitwise OR operator |

The output of bitwise OR is 1 if at least one corresponding bit


of two operands is 1. In C Programming, bitwise OR operator
is denoted by |.
EXAMPLE:
12 = 00001100 (In Binary)
25 00011001 (In Binary)
Bitwise OR Operation of 12 and 25
00001100
| 00011001
00011101 = 29 (In decimal)
Bitwise XOR (exclusive OR) operator
^
The result of bitwise XOR operator is 1 if the corresponding bits
of two operands are opposite. It is denoted by ^.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bitwise XOR Operation of 12 and 25


00001100
^ 00011001
________
00010101 = 21 (In decimal)
Bitwise complement operator ~
Bitwise compliment operator is an unary operator (works on only one operand). It
changes 1 to 0 and 0 to 1. It is denoted by ~.

Example:
#include <stdio.h>
int main()
{
printf("Output = %d\n",~35); printf("Output = %d\n",~-12);
return 0;
}

Output
Output = -36
Output = 11
ONE’S COMPLEMENT:
~ 1 -> 00000001
~1 ->1111111110 (1’s complement)
How to find the decimal equivalent of 11111110?
Since the sign bit is 1 the number is negative. To get the decimal value of
negative (-) number get the 2’s complement:
2’s complement = - (1’s complement + 1)
1’s complement of 11111110 -> 00000001 +1
---------------
00000010
00000010 decimal value -> 2. Hence the number is -2.
SHIFT OPERATORS: >> AND <<
Miscellaneous Operators
Operator Description Example

Returns the size of a sizeof(a), where a is


sizeof()
variable. integer, will return 4.

&i; returns the actual


Returns the address
& address of the
of a variable.
variable.
*i; indicate a pointer
* Pointer to a variable.
variable i.

i==1?”Y”:”N”; If
Conditional condition is true then
?:
expression. value Y otherwise
value N.
THANK YOU

You might also like