Operators in C++ language

Operators:
  • Operators are symbols and used to perform some mathematics operations.
  • There are 6 kinds of operators,
1.Arithmetic operators: 
  • This is normal mathematic operations such as,
Example program for the arithmetic operator:

#include<iostream.h>
#include <conio.h>
 void main()
 {
    int a,b;
    clrscr();
    cout<<"\n Enter a and b value:";
    cin>>a>>b;
    cout<<"\n Addition value is:"<<(a+b);
    cout<<"\n Subtraction value is:"<<(a-b);
    cout<<"\n Multiplication value is:"<<(a*b);
    cout<<"\n Division value is:"<<(a/b);
    cout<<"\n Modulo value is:"<<(a%b);
    cout<<"\n a++ value is:"<<a++;
    cout<<"\n b-- value is:"<<a--;
   getch();
}

Output:
2. Relational Operators:
  • It is used to compare two or more data.
Note:
  • These logical operators mostly used in conditions.
3. Logical Operators:
  • It is used to combine simple relational statements into more complex expressions. 
4. Assignment Operators:
  • These operators are used to getting the result in the same variable(input/output).
Example program for the assignment operator:

 #include<stdio.h>
 #include <conio.h>
 void main()
 {
    int a;
    clrscr();
    cout<<"Enter a  value:";
    cin>>a;
    a+=20;
    cout<<"a value is:"<<a; 
   getch();
}

Output:





5. Conditional Operators (? :)
  • It checks itself the condition and executes the statement depending on the condition.
Syntax:
Condition?exp1:exp2;
Example program for the conditional operator:

#include<iostream.h>
#include <conio.h>
 void main() 
 { 
    int a=5,b=3,big;
    clrscr();
big=a>b?a:b;
  cout<<"\n Biggest value is: "<<big;
  getch();
  }

Output:





6. Bitwise Operators:
  • It is used to manipulate the data at a bit level and it operates on integer only.
                                                                                  Binary table

Bitwise AND(&):
  • The truth table for ‘ & ‘is shown below(It means True=1 and false=0.)
a=7 of binary code:0111
b=8 of the binary code is:1000
The result of the a&a value is 0000(i.e 0).




Example program for the bitwise AND operator:


#include <iostream.h>
#include<conio.h>
void main() {
                   unsigned int a;
                   unsigned int b;
                   int c = 0;         
                   clrscr();
                   cout<<"\n Enter a and b value : ";
                   cin>>a>>b;
                   c = a&b;    
                   cout<<"\n Bitwise AND of a&b value is:"<<c;
                   getch();
 }

Output:





Bitwise OR(|):
  • The truth table for ‘ | ‘is shown below
a=7 of binary code:0111
b=8 of the binary code is:1000
The result of a|b value is 1111(i.e 15).




Example program for the bitwise OR operator:

#include <iostream.h>
#include<conio.h>
void main() {
                   unsigned int a;
                   unsigned int b;
                   int c = 0;         
                   clrscr();
                   cout<<"\n Enter a and b value : ";
                   cin>>a>>b;
                   c =a|b;       
                   cout<<"\n Bitwise OR of a|b value is: "<<c;
                   getch();
 }

Output:





Bitwise Exclusive OR(^):
  • The truth table for ‘ ^ ‘is shown below
a=13 of binary code:1101
b=8 of the binary code is:1000
The result of a^b value is 0101(i.e 5).



Example program for the bitwise Exclusive OR operator:

#include <iostream.h>
#include<conio.h>
void main() {
                   unsigned int a;
                   unsigned int b;
                   int c = 0;         
                   clrscr();
                   cout<<"\n Enter a and b value : ";
                   cin>>a>>b;
                    c =a^b;       
                   cout<<"\n Bitwise Exclusive OR of a^b value is: "<<c;
                   getch();
 }

Output:





Bitwise Shift left (<<):
  • The bitwise shift left operator is used to move the bits from right to left in storage.
  • Each box called bits and a=6 and a<<1 then a=12.
Explanation:







Result: a of left shit value is 12.


Example program for the bitwise shift left operator:

#include <iostream.h>
#include<conio.h>
void main() {
                   unsigned int a;
                   int c = 0;         
                   clrscr();
                   cout<<"\n Enter a  value : ";
                   cin>>a;
                   c =a<<1;    
                   cout<<"\n Bitwise left shit of a value is: "<< c;
                   getch();
}

Output:





Bitwise Shift right(>>):
  • The shit right is similar to the left shit.
  • This operator is used to forward the bits from left to right in storage.
  • For example, If a=6 and a>>2 then its value is 3.

Explanation:


Result: a of right shit value is 3.



Example program for the bitwise shift right operator:

#include <iostream.h>
#include<conio.h>
void main() {
                   unsigned int a;
                   int c = 0;         
                   clrscr();
                   cout<<"\n Enter a  value : ";
                   cin>>a;
                   c =a>>1;    
                   cout<<"\n Bitwise right shit of a value is: "<< c;
                   getch();
}

Output:
Bitwise One's complement(~):
  • The truth table for ‘ ~ ‘is shown below


If a=6(binary code is 0110),
 and ~a(0111) value is 7. 
using a negative signature for one's complement(~a=-7).


Example program for the bitwise one's complement operator:

#include <iostream.h>
#include<conio.h>
void main() {
                   unsigned int a;
                   int c = 0;         
                   clrscr();
                   cout<<"\n Enter a  value : ";
                   cin>>a;
                   c =~a;    
                   cout<<"\n Bitwise one\'s complement of a value is: "<< c;
                   getch();
}

Output:



Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Blog Archive

Service Support

Need our help to Learn or Post New Concepts Contact me

Blog Archive

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me