Binary Operators C Programming
Binary operators act upon a two operands to produce a new value. Such, operators can be classified into different categories.
Syntax for binary operator is:
operand1 operator operand2
Arithmetic Operators
Arithmetic operators are the operators used to perform the arithmetic operations like addition, subtraction, multiplication, division, and modulo operation.
Different arithmetic operators available in C programming language are tabulated below:
Symbol | Meaning | Examples |
---|---|---|
+ | Addition | a=23 and b=10 then a+b is equal to 33. |
- | Subtraction | a=23 and b=10 then a-b is equal to 13. |
* | Multiplication | a=23 and b=10 then a*b is equal to 230. |
/ | Division | a=23 and b=10 then a/b is equal to 2 (In Integer division) and 2.3 (In Real Division) |
% | Remainder or Modulo | a=23 and b=10 then a%b is equal to 3. Note: Both operand should be integer while using % operator. |
Arithmetic operators (except %) can be used in 3 modes:
Integer mode: where both the operands are integer.
Real mode: where both the operands are real.
Mixed mode: where one operand is integer and second operator is real.
Note : Result is always a floating point number for real mode and mixed mode. For integer mode, result is always an integer number.
Relational Operators
These operators are very helpful for making decisions. Depending upon the condition, it returns either 0 or 1. When the condition with these operators is true, 1 is returned. If the condition is false, it returns 0.
Different relational operators available in C programming language are tabulated with examples:
Symbol | Meaning | Examples |
---|---|---|
< | Less Than | a=2 and b=3 then a < b gives True (1) similarly b < a gives False (0) |
> | Greater Than | a=2 and b=2 then a>b gives False (0) similarly b > a gives False (0) |
<= | Less Than or Equal To | a=2 and b=2 then a <= b gives True(1) |
>= | Greater Than or Equal To | a=3 and b=4 then a >= b gives False(0) |
== | Equal To | a=9 and b=9 then a==b gives True(1) similarly if a=8 and b=9 then a==b gives False (0) |
!= | Not Equal To | a=9 and b=9 then a!=b gives False(0) similarly if a=8 and b=9 then a!=b gives True (1) |
Logical Operators
These operators are generally used along with relation operators. Like relational operators, output of these operators is either True (1) or False (0).
Different logical operators available in C programming language are tabulated with examples:
Symbol | Meaning | Examples |
---|---|---|
&& | Logical AND Rule: False&&False = False False&&True = False True&&False = False True&&True = True |
(2>3 && 4>3) Evaluates to False && True which then evaluates to False (0). |
|| | Logical OR Rule: False&&False = False False&&True = True True&&False = True True&&True = True |
(2>3 || 4>3) Evaluates to False || True which then evaluates to True (1). |
! | Logical NOT Rule: ! (False) = True ! (True) = False Note: This is Unary Operator |
!(2>3) Evaluates to !(False) which then evaluates to True (1). |
Assignment Operators
Assignment operators are used when the value is to be assigned to an identifier, a variable. With execution of assignment operators, value at the right is assigned to the left. The destination variable loses the old value; i.e. old value is over ridden with the new value. If previous value is also required; it should be saved in some other variables.
Different Assignment operators available in C programming language are tabulated with examples:
Symbol | Meaning | Examples |
---|---|---|
= | Assignment | x=2, here value 2 is assigned to x. |
+= | Addition and Assignment | a += b is equivalent to a = a+b |
-= | Subtraction and Assignment | a - = b is equivalent to a = a-b |
*= | Multiplication and Assignment | a *= b is equivalent to a = a*b |
/= | Division and Assignment | a /= b is equivalent to a = a/b |
%= | Remainder and Assignment | a %= b is equivalent to a = a%b |
Bitwise Operators
The bitwise operators are the bit manipulation operators. They can manipulate individual bits.
Different bitwise operators available in C programming language are tabulated with examples:
Symbol | Meaning | Examples |
---|---|---|
~ | Bitwise Complement Note: This is Unary Operator |
If a=01011 then ~a gives 10100 |
& | Bitwise AND | If a = 1001 and b=1111 then a&b gives 1001 |
| | Bitwise OR | If a = 1001 and b=1111 then a|b gives 1111 |
^ | Bitwise XOR | If a = 1001 and b=1111 then a^b gives 0110 |
<< | Bitwise Shift Left | If a=1010 and b=1 then a << b gives 0100 (Left shifted by 1 and 0 is inserted at last) |
>> | Bitwise Shift Right | If a=1111 and b=2 then a>>b gives 0011 (Right shifted by 2 and two 0’s are inserted at the beginning) |