Professional Documents
Culture Documents
BINARY : 2 operands
Ex: + , - ,* , / , > , < , >= ,<= , && , || etc
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Operators in C
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Special Operators
(A == B)
Checks if the values of two operands are equal or not. If
== is not
yes, then the condition becomes true.
true.
Checks if the values of two operands are equal or not. If
(A != B)
!= the values are not equal, then the condition becomes
is true.
true.
Checks if the value of left operand is greater than the (A > B)
> value of right operand. If yes, then the condition becomes is not
true. true.
Checks if the value of left operand is less than the value (A < B)
<
of right operand. If yes, then the condition becomes true. is true.
Checks if the value of left operand is greater than or (A >= B)
>= equal to the value of right operand. If yes, then the is not
condition becomes true.
Akshata Angadi, Assistant Professor, CSE Dept., GIT true.
Back
Logical Operators
If A = 56 and B = 0
Operator Descripton Example
Called Logical AND operator. If both the
&& operands are non-zero, then the (A && B) is false.
condition becomes true.
Called Logical OR Operator. If any of the
|| two operands is non-zero, then the (A || B) is true.
condition becomes true.
Called Logical NOT Operator. It is used to
reverse the logical state of its operand. If
! !(A && B) is true.
a condition is true, then Logical NOT
operator will make it false.
Back
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Bitwise Operators
Operator Description
Binary AND Operator copies a bit to the result if
&
it exists in both operands.
Binary OR Operator copies a bit if it exists in
|
either operand.
Binary XOR Operator copies the bit if it is set in
^
one operand but not both.
Binary Ones Complement Operator is unary and
~
has the efect of 'fipping' bits.
Binary Left Shift Operator. The left operands
<< value is moved left by the number of bits
specifed by the right operand.
Binary Right Shift Operator. The left operands
>> value is moved right by the number of bits
specifed by the right operand.
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Exercise
• Bitwise AND operation of two integers 12 and 25.
12 -> 0000 1100
25 -> 0001 1001
X Y X&Y
8 -> 0000 1000 (result)
0 0 0
0 1 0
1 0 0
result = 12 & 25
1 1 1
0 1 1
1 0 1
1 1 1
0 1 1
1 0 1
Note: Bitwise complement of
1 1 0
any number ~N is -(N+1).
Answer : 30 – ( 011110 )
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Right Shift ( >> )
A >> 3 , Given A = 60
60- 111100 Binary Equivalent
Shift at right by 3 , last element is 0
000111
= 2^2+2^1+2^0
= 4+2+1
= 7
Answer : 7
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Example : A=60, B=13
(A & B) = 12,
i.e., 0000 1100
(A | B) = 61,
i.e., 0011 1101
(A ^ B) = 49,
i.e., 0011 0001
(~A ) = -61,
i.e,. 1100 0011 in 2's complement form.
A << 2 = 240
i.e., 1111 0000
A >> 2 = 15
i.e., 0000 1111
Akshata Angadi, Assistant Professor, CSE Dept., GIT Back
#include <stdio.h>
main( ) { //unsigned means it wont take negative values for a & b
unsigned int a = 60; /* 60 = 0011 1100 */
unsigned int b = 13; /* 13 = 0000 1101 */
int c = 0;
c = a & b; /* 12 = 0000 1100 */
printf("Line 1 - Value of c is %d\n", c );
c = a | b; /* 61 = 0011 1101 */
printf("Line 2 - Value of c is %d\n", c );
c = a ^ b; /* 49 = 0011 0001 */
printf("Line 3 - Value of c is %d\n", c );
c = ~a; /*-61 = 1100 0011 */
printf("Line 4 - Value of c is %d\n", c );
c = a << 2; /* 240 = 1111 0000 */
printf("Line 5 - Value of c is %d\n", c );
c = a >> 2; /* 15 = 0000 1111 */
printf("Line 6 - Value of c is %d\n", c ); Assistant Professor, CSE Dept., GIT
Akshata Angadi,
Assignment Operators
Shorthand Operators
=, += , -= , *= , /= , %= , <<= , >>=
&= , |= (IN-OR) , ^= (EX-OR) ,
Example:
a+=b means a = a + b
x/=g means x = x / g
d&=c means d = d & c
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Misc or special Operators
• Conditional Operator ( ;? )
• Asterisk (*) Operator
• sizeof( ) Operator
Operato
Description Example
r
sizeof(a), where a is integer,
sizeof( ) Returns the size of a variable.
will return 4.
Returns the address of a &a; returns the actual
&
variable. address of the variable.
a=10;
What's the Output
(a!=0) ? False : True ;
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Precedence of Operators
n = (23 * x ) / (r – h + t )
2.
3.