Lecture-6(Operators and Expressions)
Lecture-6(Operators and Expressions)
Assignment Operators
It is used to assign a particular value to a variable. = (Assignment)- Used to assign a value from
right side operand to left side operand.
1. += (Addition Assignment)- To store the sum of both the operands to the left side operand.
2. -= (Subtraction Assignment) – To store the difference of both the operands to the left side
operand.
3. *= (Multiplication Assignment) – To store the product of both the operands to the left side
operand.
4. /= (Division Assignment) – To store the division of both the operands to the left side
operand.
5. %= (Remainder Assignment) – To store the remainder of both the operands to the left side
operand.
result = 20
result = 10
result = 100
result = 10
result = 0
5. Bitwise Operators
It is based on the principle of performing operations bit by bit which is based on boolean
algebra. It increases the processing speed and hence the efficiency of the program.
In order to clearly understand bitwise operators, let us see the truth table for various bitwise
operations and understand how it is associated with boolean algebra.
Since there are 2 variables, namely, a and b, there are 22 combinations for values a and b can
take simultaneously.
AND – Both the operands should have boolean value 1 for the result to be 1.
OR – At least one operand should have boolean value 1 for the result to be 1.
XOR (EXCLUSIVE OR) – Either the first operand should have boolean value 1 or the second
operand should have boolean value 1. Both cannot have the boolean value 1 simultaneously.
One Complement: bitwise and, or, xor and not truth table values as follows :
a = 26 and b=14
Bitwise AND
a = 26 = 1 1 0 1 0
b = 14 = 0 1 1 1 0
________
a & b = 0 1 0 1 0 which is equal to 10
Bitwise OR
a = 26 = 1 1 0 1 0
b = 14 = 0 1 1 1 0
________
a | b = 1 1 1 1 0 which is equal to 30
Bitwise XOR
a = 26 = 1 1 0 1 0
b = 14 = 0 1 1 1 0
________
a | b = 1 0 1 0 0 which is equal to 20
#include <stdio.h>
int main()
{
int a = 26, b = 14;
printf(" Bitwise AND operation %d & %d : %d\n",a,b,a&b);
printf(" Bitwise OR operation %d | %d : %d\n",a,b,a|b);
printf(" Bitwise XOR operation %d ^ %d : %d\n",a,b,a^b);
return 0;
}
Output
Bitwise AND operation 26 & 14 : 10
Bitwise OR operation 26 | 14 : 30
Bitwise XOR operation 26 ^ 14 : 20
Bitwise OR:
Converts the value of both the
| a, b (a|b)
operands into binary form and
performs OR- operation bit by bit
6. Miscellaneous Operators
Apart from the above-discussed operators, there are certain operators which fall under this
category which include sizeof and ternary (conditional) operators.
1. sizeof – It returns the memory occupied by the particular data type of the operand
2. & (Pointer) – It refers to the address (memory location) in which the operand is stored.
3. * (Pointer) – It is a pointer operator
4. ? (Condition) – It is an alternative for if-else condition