OPERATORS IN C LANGUAGE
OPERATORS IN C
An operator is a symbol that tells the compiler to perform specific mathematical or logical functions.
C language is rich in built-in operators
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Additional Operators
Arithmetic Operators:
Basic Arithmetic operators include
Operator Description Example
+ Adds two operands. A + B = 30
− Subtracts second operand from the first. A − B = -10
* Multiplies both operands. A * B = 200
/ Divides numerator by de-numerator. B / A = 2
% Modulus Operator(Returns remainder of a division) B % A = 0
++ Increment operator increases the integer value by one. A++ = 11
-- Decrement operator decreases the integer value by one. A-- = 9
Relational Operators :
Operator Function
== Checks if the values of two operands are equal or not. If yes, then the condition
becomes true.
!= Checks if the values of two operands are equal or not. If the values are not
equal, then the condition becomes true.
> Checks if the value of left operand is greater than the value of right operand. If
yes, then the condition becomes true.
< Checks if the value of left operand is less than the value of right operand. If yes,
then the condition becomes true.
>= Checks if the value of left operand is greater than or equal to the value of right
operand. If yes, then the condition becomes true.
<= Checks if the value of left operand is less than or equal to the value of right
operand. If yes, then the condition becomes true.
Logical operators:
The three main logical operators are ‘&&’, ‘||’ and ‘!’.
&& --Called Logical AND operator. If both the operands are non-zero, then the condition becomes true.
|| --Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true.
! --Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.
Bitwise Opearators:
In the C programming language, operations can be performed on a bit level using bitwise operators.
C provides six operators for bit manipulation
Bitwise operators are used to perform manipulation of individual bits of a number.
They can be used with any of the integral types (char, short, int, etc).
& bitwise AND
| bitwise inclusive OR
^ bitwise XOR (exclusive OR)
<< left shift
>> right shift
~ bitwise NOT (one's complement) (unary)
So for performing bitwise operators you have to know the conversion of decimal into binary
DECIMAL TO BINARY :
we take example as 5
Binary -> combination of 0's and 1's
1)Divide 5 with 2---remainder is 1
2)now divide 2 with 2---remainder 0
3)now divide 1 with 2 ---remainder is 1
4)now take remainders from bottom to top= 101
5 in binary form represented as 101
BINARY TO DECIMAL
So we take example as 1011
Now we have to convert into decimal value
We will start from right side
1* 2 power 0+1* 2 power 1+0* 2 power 2+1* 2power 3
=1+2+0+8
=11
Bitwise AND (&) : AND operator is used when if it exists in both operands
bit a bit b a & b (a AND b)
0 0 0
0 1 0
1 0 0
1 1 1
Ex: 5 & 4
5->101
4->100
----------
100
---------
Bitwise OR(|) : OR operator is used when if it exists in either operand.
bit a bit b a | b (a OR b)
0 0 0
0 1 1
1 0 1
1 1 1
Ex: 11001000
| 10111000
--------
= 11111000
--------
Bitwise XOR : The bitwise XOR (exclusive or) performs a logical XOR function, which is equivalent to adding two bits and discarding the carry.
The result is zero only when we have two zeroes or two ones
bit a bit b a ^ b (a XOR b)
0 0 0
0 1 1
1 0 1
1 1 0
Ex : 11001000
^ 10111000
--------
= 01110000
Bitwise NOT ~ / ones' complement :The ones' complement (~) or the bitwise complement gets us the complement of a given number.
Thus we get the bits inverted, for every bit 1 the result is bit 0 and conversely for every bit 0 we have a bit 1.
This operation should not be confused with logical negation !
bit a ~a (complement of a)
0 1
1 0
Ex: ~ 11001000
--------
= 00110111
Shift operators :
There are two bitwise shift operators. They are
Left shift (<<)
Right shift (>>)
Left shift (<<) : Here we have to mention bits
suppose a=4 and b=3
a<<2(value of a with 2 bits)
This will be done in 8 bit representation
4-> 0000 0100
2 bits should move on left side
0000 0100
except first two bits move that bits and write that two bits in last
00 010000----decimal value is 16
The result is a<<2 is 16
Right Shift (>>)
Here also we have to mention bits
suppose a=4 and b=3
a>>2(value of with 2 bits)
4->0000 0100
so ignore the last two digits and write as 0000 01
and keep that digits in first now value is 00 0000 01
The resultant value is a>>2 is 1
Assignment Operators :
= Simple assignment operator. Assigns values from right side operands to left side operand
+= Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. C += A is equivalent to C = C + A
-= Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand.
C -= A is equivalent to C = C - A
*= Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand.
C *= A is equivalent to C = C * A
/= Divide AND assignment operator.
It divides the left operand with the right operand and assigns the result to the left operand. C /= A is equivalent to C = C / A
%= Modulus AND assignment operator.
It takes modulus using two operands and assigns the result to the left operand.
C %= A is equivalent to C = C % A
<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2
^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2
|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2
Additional Operators :
sizeof() Returns the size of a variable. sizeof(a), where a is integer, will return 4.
& Returns the address of a variable. &a; returns the actual address of the variable.
* Pointer to a variable. *a;
? : Conditional Expression. If Condition is true ? then value X : otherwise value Y
Comments
Post a Comment