KCA102 Unit1- Operators (1)
KCA102 Unit1- Operators (1)
So, the expression 121 + 17 - 110 is a constant expression because each of the terms of
the expression is a constant value.
But if i were declared to be an integer variable, the expression 180 + 2 – j would not
represent a constant expression.
Operator
C programming language offers various types of operators having different functioning
capabilities.
1. Unary Operators
2. Arithmetic Operators
3. Relational Operators
4. Logical Operators
5. Assignment Operators
6. Increment and Decrement Operators
7. Conditional Operator
8. Bitwise Operators
9. Special Operators
Operator is a symbol used to perform some operation on variables, operands or with the
constant. Some operator required 2 operands to perform operation or Some required single
operation.
In C programming Language there are several types operators like arithmetic operator,
assignment, increment, decrement, logical, conditional, comma, sizeof , bitwise and others.
1. Unary Operators – Operated on one Operand
2. Binary Operators- operated on at least two operands
3. Ternary or Conditional Operator Special Type of Operator condition evaulation
1. Unary Operator:
Unary Operators are special type of Operators, operate on one operand
Following Unary Operators are used in C programming language.
SNo Operators Symbols
2 Unary minus -
3 Increment operator ++
4 Decrement operator --
5 Address of Operator &
6. Logical Negation !
A=01001000 B=
10111000
01001000 &
10111000 =
A&B
---------------
00001000
The Bitwise OR (|) in C: The C compiler recognizes the Bitwise OR with | operator.
It takes two operands and performs the OR operation for every bit of the two operand
numbers. It is also a binary operator. The output of this operator will result in 1 if any
one of the two bits is 1.
Example 72 | 184 = 248
1001000 |
10111000 =
--------------
11111000
The Bitwise XOR (^) in C: The C compiler recognizes the Bitwise XOR with ^
operator. It takes two operands and performs the XOR operation for every bit of the
two operand numbers. It is also a binary operator. The output of this operator will
result in 1 if both the bits have different values.
Binary One’s Complement or Bitwise NOT operator (~) in C: The C compiler
recognizes the Bitwise NOT with ~ operator. It takes only one operand and performs
the inversion of all digits of it. It is a unary operator. The output of this operator will
invert all the existing bits of that operand.
Bitwise Left shift operator (<<) in C: The C compiler recognizes the left shift
operation with this <<. It takes only two operands and shifts all the bits of the first
operand to the left. The second operand decides how many numbers of places this
operator will shift its bits. It is a binary operator.
Bitwise Right shift operator (>>) in C: The C compiler recognizes the left shift
operation with this >>. It takes only two operands and shifts all the bits of the first
operand to the right. The second operand decides how many numbers of places this
operator will shift its bits. It is a binary operator.
TRUTH TABLE FOR BIT WISE OPERATION & BIT WISE OPERATORS:
Example
Consider x=40 and y=80. Binary form of these values are given below.
x= 00101000
y= 01010000
X|Y= 01111000
X&Y 00000000
All bit wise operations for x and y are given below.
X&y = 00000000 (binary) = 0 (decimal)
X|y = 01111000 (binary) = 120 (decimal)
~x = Bit wise NOT :
Value of 40 in binary is 00000000000000000000000000000000
So, all 0’s are converted into 1’s in bit wise NOT operation.
x=101000
~ 010111
x^y = 01111000 (binary) = 120 (decimal)
Bit wise left shift and right shift : In left shift operation “x << 1 “, 1 means that the bits
will be left shifted by one place. If we use it as “x << 2 “, then, it means that the bits will
be left shifted by 2 places.
x=00101000
x << 1 = 01010000 (binary) = 80 (decimal)
x >> 1 = 00010100 (binary) = 20 (decimal)
#include <stdio.h>
int main()
{
int x = 40, y = 80,AND_op,OR_op,XOR_op,NOT_op ;
AND_op = (x&y);
OR_op = (x|y);
NOT_op = (~x);
XOR_op = (x^y);
printf("AND_op value = %d\n",AND_op );
printf("OR_op value = %d\n",OR_op );
printf("NOT_op value = %d\n",NOT_op );
printf("XOR_op value = %d\n",XOR_op );
printf("left_shift value = %d\n", x << 1);
printf("right_shift value = %d\n", x >> 1);
}
Example program using all the bitwise operators.
#include <stdio.h>
int main()
{
int x = 20, y = 21; // x = 20 (00010100), y = 21 (00010101)
int r = 0;
r = x & y; /* 20 = 010100 */
printf(” Result of Bitwise AND is %d \n”, r );
r = x | y; /* 21 = 010101 */
printf(”Result of Bitwise OR is %d \n”, r );
r = x ^ y; /* 1 = 0001 */
printf(” Rresult of Bitwise XOR is %d \n”, r );
r = ~x;
printf(” Result of Bitwise NOT is %d \n”, r );
r = x << 1;
printf(” Result of Bitwise Left Shift is %d \n”, r );
r = x >> 1;
printf(” Result of Bitwise Right Shift is %d \n”, r );
return 0;
}
OUTPUT:
The result of Bitwise AND is 20
The result of Bitwise OR is 21
The result of Bitwise XOR is 1
The result of Bitwise NOT is -21
The result of Bitwise Left Shift is 40
The result of Bitwise Right Shift is 10
References
1. Hanly J. R. and Koffman E. B.,“Problem Solving and Program Design in C”, Pearson Education.
2. Schildt H., “C- The Complete Reference”, McGraw-Hill.
3. Kanetkar Y., “Let Us C”, BPB Publications.
4. Gottfried B., “Schaum’s Outlines- Programming in C”, McGraw-Hill Publications.
5. Kochan S.G., “Programming in C”, Addison-Wesley.
6. Dey P. and Ghosh M., “Computer Fundamentals and Programming in C”, Oxford University Press.
7. Goyal K. K., Sharma M. K. and Thapliyal M. P. “Concept of Computer and C Programming”,
University Science Press.
8. Goyal K. K. and Pandey H.M., Trouble Free C”, University Science Press