Basics of Operators

Operators
Operators are symbols that tell the compiler to perform specific mathematical or logical manipulations. In this tutorial, we will try to cover the most commonly used operators in programming.

First, let's categorize them:
1. Arithmetic
2. Relational
3. Logical
4. Bitwise
5. Assignment
6. Increment
7. Miscellaneous

 

Arithmetic Operators

Computer programs are widely used for mathematical calculations. We can write a computer program which can do simple calculation like adding two numbers (2 + 3) and we can also write a program, which can solve a complex equation like P(x) = x4 + 7x3 - 5x + 9. If you have been even a poor student, you must be aware that in first expression 2 and 3 are operands, and + is an operator. Similar concepts exist in Computer Programming.

 

Operator              Description                                                                 Example

 

+                     Adds two operands                                                         A + B will give 30

-                      Subtracts second operand from the first                         A - B will give -10

*                     Multiplies both operands                                                A * B will give 200

/                      Divides numerator by de-numerator                               B / A will give 2

%                    This gives a remainder of an integer division                 B % A will give 0

 

 

Relational Operators

Consider a situation where we create two variables and assign them some values as follows −

A = 20

B = 10

Here, it is obvious that variable A is greater than B in values. So, we need the help of some symbols to write such expressions which are called relational expressions. If we use C programming language, then it will be written as follows −

(A > B)

Here, we used a symbol > and it is called a relational operator and in their simplest form, they produce Boolean results which means the result will be either true or false. Similarly, a programming language provides various relational operators. The following table lists down a few of the important relational operators available in C programming language. Assume variable A holds 10 and variable B holds 20, then

 

Operator       Description                                                                            Example

 

==                 Checks if the values of two operands are equal or not,

                     if yes hen condition becomes true.                                         

                                                                                                                   (A == B) is not true.

 

!=                  Checks if the values of two operands are equal or not,

                     if values are not equal then condition becomes true.               

                                                                                                                   (A != B) is true.

 

>                   Checks if the value of left operand is greater than the

                      value of right operand, if yes then condition becomes true.

                                                                                                                   (A > B) is not true.

 

<                   Checks if the value of left operand is less than the value

                     of right operand, if yes then condition becomes true.              

                                                                                                                   (A < B) is true.

 

>=                 Checks if the value of left operand is greater than or equal

                     to the value of right operand, if yes then condition becomes true.

                                                                                                                   (A >= B) is not true.

 

<=                 Checks if the value of left operand is less than or equal

                     to the value of right operand, if yes then condition becomes true.     

                                                                                                                   (A <= B) is true.

 

 

Logical Operators

Logical operators are very important in any programming language and they help us take decisions based on certain conditions. Suppose we want to combine the result of two conditions, then logical AND and OR logical operators help us in producing the final result.

The following table shows all the logical operators supported by the C language. Assume variable A holds 1 and variable B holds 0, then −

Operator    Description                                                                                Example

 

&&           Called Logical AND operator. If both the

                   operands are non-zero, then condition becomes true.                    (A && B) is false.

 

||               Called Logical OR Operator. If any of the two

                   operands is non-zero, then condition becomes true.                      (A || B) is true.

 

!                Called Logical NOT Operator. Use to reverses

                 the logical state of its operand. If a condition is true then

                   Logical NOT operator will make false.                                         !(A && B) is true.

Bitwise Operators

These operators are very useful and we have some tricks based on these operators. These operators convert the given integers into binary and then perform the required operation, and give back the result in decimal representation.

Symbol         Operation           Usage         Explanation

&                  bitwise AND       x & y          Sets the bit to the result if it is set in both operands.

|                    bitwise OR         x | y            Sets the bit to the result if it is set in either operand.

^                   bitwise XOR       x ^ y           Sets the bit if it is set in one operand but not both

~                  bitwise NOT       ~x              Unary operator and has the effect of 'flipping' bits,i.e,                                                                              flips 1 to 0 and 0 to 1.

<<                left shift             x << y         The left operand's value is moved left by the number                                                                    of bits specified by the right operand. It is equivalent                                                                     to multiplying x by 2y

>>                right shift           x >> y         The left operand's value is moved right by the number                                                                   of bits specified by the right operand. It is equivalent                                                                                                to dividing x by 2y

         

Assignment Operators:

Symbol

Operation

Usage

Explanation

=

assignment

x = y

Assigns value from the right side

operand(s) to the left side operand.

+=

add and assignment

x += y

Adds the right side operand to the left

side operand and assigns the result to

the left side operand.

-=

subtract and assignment

x -= y

Subtracts the right side operand from

the left side operand and assigns the

result to the left side operand.

*=

multiply and assignment

x *= y

Multiplies the right side operand with

the left side operand and assigns the

result to the left side operand.

/=

divide and assignment

x /= y

Divides the left side operand with the

right side operand and assigns the

result to the left side operand.

%=

modulus and assignment

x%=y

Takes modulus using the two

operands and assigns the result to

the left side operand.

<<=

left shift and assignment

x<<=y

Shifts the value of x by y bits towards

the left and stores the result back in

x.

>>=

right shift and assignment

x>>=y

Shifts the value of x by y bits towards the

right and stores the result back in

x.

&=

bitwise AND and assignment

x&=y

Does x&y and stores result back in x.

|=

bitwise OR and assignment

x|=y

Does x|y and stores result back in x

^=

bitwise XOR and assignment

x^=y

Does x^y and stores result back in x.

 

 

Increment/Decrement Operators:

These are unary operators. Unary operators are the operators which require only one operand.

Symbol    Operation          Usage    Explanation

++           Postincrement    x++        Increment x by 1 after using its value

--             Postdecrement   x--         Decrement x by 1 after using its value

++           Preincrement     ++x        Increment x by 1 before using its value

--             Predecrement     --x         Decrement x by 1 before using its value

 

 ||Download File||











Comments