C ++ Operators


C ++ Operators


What is an operator?


An Operator is a symbol that is used to perform mathematical operations.


Types of operators :

Followings are the most commonly used data types in C.


Operator Name Operators
Assignment =
Arithmetic +, -, *, /, %
Logical &&, ||, !
Relational <, >, <=, >=, ==, !=
Shorthand +=, -=, *=, /=, %=
Unary ++, --
Conditional ()?:;
Bitwise &, |, ^, <<, >>, ~



Arithmetic Operators :

There are 5 arithmetic operators:
+
-
*
/
%

It is also called as 'Binary operators'. It is used to perform arithmetical operations. These operators operate on two operands.


//Program to demonstrate arithmetic operators

#include <stdio.h>
#include
<conio.h>

void main()
{
int n1,n2;
int sum;
int sub;
int mul;
int div;
int mod;

clrscr();

printf("\n\t Enter First Number :");
scanf("%d",&n1);

printf("\n\t Enter Second Number :");
scanf("%d",&n2);

sum = n1 + n2;
printf("\n\n\t Addition is : %d",sum);

sub = n1 - n2;
printf("\n\n\t Subtraction is : %d",sub);

mul = n1 * n2;
printf("\n\n\t Multiplication is : %d",mul);

div= n1/ n2;
printf("\n\n\t Division is : %d",div);

mod = n1 % n2;
printf("\n\n\t Modulus is : %d",mod);

getch();
}


Assignment Operators :

It is used to assign a value to variable.


//Program to demonstrate assignment operator


#include <stdio.h>
#include
<conio.h>

void main()
{
int a,b;

clrscr();

a = 53;

printf("\n\t Value of A : %d",a); // 53

b = a; // Interchange of value using assignment

printf("\n\n\t Value of B : %d",b); // 53

getch();
}


Logical Operators :

Sometimes, we have to check more than one condition at a time then it is operator which is primarily used to check more than two conditions. This operator returns 1 if condition is true otherwise 0.


//Program to demonstrate logical operators


#include <stdio.h>
#include
<conio.h>

void main()
{
int x=2;
int y=2;

clrscr();

printf("\n\n %d",(x && y)); // returns 1
printf("\n\n %d",(x|| y)); // returns 1

getch();
}


Relational Operators :

It is also used to check conditions. These operators return 1 if condition is true otherwise 0.


//Program to demonstrate Relational operators


#include <stdio.h>
#include
<conio.h>

void main()
{
int a=6;
int b=2;

clrscr();

printf("\n\n A<=B : %d",(a<=b)); // 0 - False

printf("\n\n A>B : %d",(a>b)); // 1 - True

printf("\n\n A!=B : %d",(a!=b)); // 1 - True

getch();
}


Shorthand Operators :

It is used to perform mathematical operations at which the result or output can affect on operands.


//Program to demonstrate shorthand operators .


#include <stdio.h>
#include
<conio.h>

void main()
{
int a,b;

clrscr();

a = 18;
b = 4;

printf("\n\t Value of A : %d",a); // 18
printf("\n\t Value of B : %d",b); // 4

b += a ; // b = b + a

printf("\n\n\t Using += (i.e b=b+a): %d",b); // 22

// Change the operator as -=, *=, /=, %=

getch();
}


Unary Operators :


It operates on a single operand. Therefore, this operator is called as 'unary operator.'
It is used to increase or decrease the value of variable by 1. It is also called as increment/decrement operators.


// Program to demonstrate Unary / Ternary operators .

#include <stdio.h>
#include
<conio.h>

void main()
{
int a=4, b;

clrscr();

printf("\n\n Value of A : %d",a); // 4

a++; // Post

printf("\n\n Value of A : %d",a); // 5

++a; // Pre

printf("\n\n Value of A : %d",a); // 6

b=--a;

printf("\n\n Value of A : %d",a); // 5

printf("\n\n Value of B : %d",b); // 5

b=a++;

printf("\n\n Value of A : %d",a); // 6

printf("\n\n Value of B : %d",b); // 5

b++;

printf("\n\n Value of B : %d",b); // 6

getch();
}


Conditional Operator :

Conditional operator is also called as 'ternary operator.' It is widely used to execute condition in true part or in false part.
It operates on three operands. The logical or relational operator can be used to check conditions.


The syntax of the conditional operator is

e1 ? e2 : e3

and what happens is that e1 is evaluated, and if it's true then e2 is evaluated and becomes the result of the expression, otherwise e3 is evaluated and becomes the result of the expression.
In other words, the conditional expression is sort of an if/else statement buried inside of an expression.

//Program to demonstrate conditional operator


#include <stdio.h>
#include
<conio.h>

void main()
{


int x=5;
int y=2;

int result;

clrscr();

result=(x>y)?(x+y):(x-y); //here we have used conditional operator, here (x>y) is
//true so (x+y) will be performed

printf("result is %d",result); //it will print 7 since x+y=7

getch();
}


Bitwise Operators :

Bitwise AND

The bitwise AND operator is a single ampersand: &. A handy mnemonic is that the small version of the boolean AND, &&, works on smaller pieces (bits instead of bytes, chars, integers, etc). In essence, a binary AND simply takes the logical AND of the bits in each position of a number in binary form.

For instance, working with a byte (the char type):

01001000 &
10111000 =
--------
00001000

The most significant bit of the first number is 0, so we know the most significant bit of the result must be 0; in the second most significant bit, the bit of second number is zero, so we have the same result. The only time where both bits are 1, which is the only time the result will be 1, is the fifth bit from the left. Consequently,

72 & 184 = 8

Bitwise OR

Bitwise OR works almost exactly the same way as bitwise AND. The only difference is that only one of the two bits needs to be a 1 for that position's bit in the result to be 1. (If both bits are a 1, the result will also have a 1 in that position.) The symbol is a pipe: |. Again, this is similar to boolean logical operator, which is ||.

01001000 |
10111000 =
--------
11111000

and consequently

72 | 184 = 248


The Bitwise Complement

The bitwise complement operator, the tilde, ~, flips every bit. A useful way to remember this is that the tilde is sometimes called a twiddle, and the bitwise complement twiddles every bit: if you have a 1, it's a 0, and if you have a 0, it's a 1.

Note that ~ and ! cannot be used interchangeably. When you take the logical NOT of a non-zero number, you get 0 (FALSE). However, when you twiddle a non-zero number, the only time you'll get 0 is when every bit is turned on.


Bitwise Exclusive-Or (XOR)

There is no boolean operator counterpart to bitwise exclusive-or, but there is a simple explanation.

The exclusive-or operation takes two inputs and returns a 1 if either one or the other of the inputs is a 1, but not if both are. That is, if both inputs are 1 or both inputs are 0, it returns 0. Bitwise exclusive-or, with the operator of a carrot, ^, performs the exclusive-or operation on each pair of bits.

Exclusive-or is commonly abbreviated XOR.

For instance, if you have two numbers represented in binary as 10101010 and 01110010 then taking the bitwise XOR results in 11011000. It's easier to see this if the bits are lined up correctly:
01110010 ^
10101010
--------
11011000




     Prev                                                   NEXT

0 comments:

Post a Comment