You can
combine multiple expressions in a single expression using the comma operator.
Program
#include<stdio.h>
main()
{
int i,j,k;
k = (i = 4,
j = 5);
printf("k = %d",k);
}
Input
i = 4,j = 5.
Output
k = 5.
Explanation
For
example, you can write: k = (i =
4, j = 5)
Here the
expression is evaluated from left to right, that is, i = 4 is evaluated first then j = 5 is evaluated. The value of the rightmost
expression is specified as output, thus k will get
the value 5.
Bitwise
operators interpret operands as strings of bits. Bit operations are performed
on this data to get the bit strings. These bit strings are then interpreted
according to data type. There are six bit operators: bitwise AND(&), bitwise OR(|), bitwise
XOR(^), bitwise complement(~), left shift(<<), and right
shift(>>).
Program
# include<stdio.h>
main()
{
char
c1,c2,c3;
printf("ENTER VAULES OF c1 and c2");
scanf("%c,%c",&c1,&c2);
c3 = c1
& c2;
printf("\n Bitwise AND i.e. c1 & c2 = %c",c3);
c3 = c1 |
c2;
printf("\n Bitwise OR i.e. c1 | c2 = %c",c3);
printf("\n Bitwise XOR i.e. c1 ^ c2 = %c",c3);
c3 = ~c1;
printf("\n ones complement of c1 = %c",c3);
c3 =
c1<<2;
printf("\n left shift by 2 bits c1 << 2 = %c",c3);
c3 =
c1>>2;
printf("\n right shift by 2 bits c1 >> 2 = %c",c3);
}
Input
c1 = 4;
c2 = 6;
Output
ENTER VALUES OF c1 and c2
4
6
Bitwise AND i.e. c1 & c2 = 4
Bitwise OR i.e. c1 | c2 = 6
Bitwise XOR i.e. c1 ^ c2 = 2
ones compliment of c1 = -4
left shift by 2 bits c1 << 2 = 16
right shift by 2 bits c1 >> 2 = 1
Explanation
- Suppose you write
- char c1, c2, c3;
- c1 = 4;
- c2 = 6;
The binary values are
c1 = 0000 0100
c2 = 0000 0110
- Suppose you write
- c3 = c1 & c2;
The value of c3 is
interpreted as follows:
0000 0100
& 0000 0110
----------.
0000 0100
Each bit of c1 is compared with the corresponding bit of c2. If both bits are 1 then the corresponding bit is set as 1, otherwise
it is set as 0. Thus the value of c3 is 4.
- c3 = c1 | c2
The value of c3 is
interpreted as follows:
0000 0100
| 0000 0110
-----------.
0000 0110
Each bit of c1 is
compared with the corresponding bit of c2. If any
of the bits are 1 then the corresponding bit is set as 1; otherwise it is set
as 0. Thus the value of c3 is 6.
- c3 = c1 ^ c2
The value of c3 is
interpreted as follows:
0000 0100
^ 0000 0110
----------.
0000 0010
Each bit of c1 is compared with the corresponding
bit of c2. If only one bit is 1, the corresponding bit is set to 1; otherwise
it is set to 0. Thus you will get the value of c3 as 2 because in the second position for c1, the bit is 0 and for c2, the bit
is 1. So only one bit is set.
- c3 = ~ c1
The value of c3 is
interpreted as follows:
~ 0000 0100
----------.
1111 1011
Each bit of c1 is
complemented; for 1 the complement is 0. Thus you will get the value of c3 as −4, because the leftmost bit is set as 1.
- c3 = c1 << 2;
This is a left-shift operation. The bits are
shifted left by two places. c1
indicates the operand that should be an expression returning a whole number. 2
indicates a shift that should not be negative, and its value must be less than
the number of bits allocated to that data type.
It is evaluated as follows:
c1 is 0000 0100
0001 00**
While shifting, the high-order (left) bits are
discarded. Since a vacuum is created on the right side, it is filled with 0s to
get 0001 0000. Thus the value is 16.
- c3 = c1 >> 2;
This is a right shift operation. The bits are
shifted right by two places. c1
indicates the operand that should be an expression returning a whole number. 2
indicates a shift that should not be negative, and its value must be less than
the number of bits allocated to that data type.
It is evaluated as follows:
c1 is 0000 0100
It is shifted 2 bits to the right to produce
**00 0001
While shifting, the low-order (right) bits are
discarded. The asterisks are replaced using one of the following strategies:
Logical shift: In this case, the high-order bits are filled with
0s, thus you get 0000 0001.
Arithmetic shift: In this case the high-order bits are filled with
the original sign bits, so if the sign bit is 1, then all bits are filled with
1s; otherwise, they are filled with 0s.
For
unsigned data types, logical shift is used, whereas for signed data types
arithmetic shift is used.
In these
examples, the char data type which is signed is
used. In number 4, the sign bit is 0, so you will get the bit pattern 0000 0001
(decimal 1).
Since C
has various types of operators, it also sets precedence rules so that the value
of expressions that involve multiple operators should be deterministic.
Program
Operators
|
Order
of evaluation
|
Remarks
|
[] ( )
->
|
Left to
right
|
Array
subscript, function call
|
− +
sizeof( ) ! ++ −−
|
||
& *
~ (cast)
|
Right
to left
|
Unary
|
* / %
|
Left to
right
|
Binary
Multiplicative
|
+ -
|
Left to
right
|
Binary
Additive
|
>>
<<
|
Left to
right
|
Shift
operators
|
<
<= > >=
|
Left to
right
|
Relational
operators
|
== !=
|
Left to
right
|
Equality
operators
|
&
|
Left to
right
|
Bitwise
And operator
|
^
|
Left to
right
|
Bitwise
Xor operator
|
|
|
Left to
right
|
Bitwise
Or operator
|
&&
|
Left to
right
|
Logical
And operator
|
||
|
Left to
right
|
Logical
Or operator
|
?:
|
Left to
right
|
Conditional
operator
|
= += -=
*= /= %=
|
||
&=
-= |= <<= >>=
|
Right
to left
|
Assignment
|
,
|
Right
to left
|
Comma
|
Point to Remember