C++ Operators

The C++ operators are used to perform various operations on the data value. Each operator has different symbols and each symbol has a different meaning in the language compiler, when the C++ code contain this symbol, then compiler will perform specific mathematical or logical manipulations.

C++ has rich set of operator

   1. I/O operator
   2. Arithmetic Operators
   3. Relational Operators
   4. Logical Operators
   5. Bitwise Operators
   6. Assignment Operators
   7. Misc Operators

I/O operator

The I/O operator typically consists of two operators cin and cout where cin stands for standard input and cout stands for standard output.

Operator Description Example
<< This is known as output operator, which is used to write the value of the standard output. Cout<<2+5; will print output "7"
>> This is known as the input operator, which is used to read the value from standard input. cin>>n; will read value

Arithmetic Operators

C++ arithmetic operators are used to perform mathematical calculations. There are five basic operator : addition, subtraction, multiplication, division, modulus.

Assume variable A is 5 and variable B is 10 -

Operator Description Example
+ "+" Is an additional operator that is used to Add two or more values( or operands). A + B result 15
- "-" Is an Subtraction operator that is used to Subtracts two or more values( or operands). A - B result -5
* "*" Is an multiplication operator that is used to Multiply two or more values( or operands). A * B result 50
/ "/" Is an division operator that is used to Divides two or more values( or operands). B / A result 2
% "%" Is an modulus operator that is used to Modulus two or more values( or operands). B % A result 0
++ "++" It is used to increases integer value by one. A++ result 5
-- "--" It is used to decreases integer value by one. B-- result 9

Relational Operators

C++ relational operators are used to describe the relationship between two operands. There are six basic relational operator : <(less than), >(greater than), <=(less than or equal to), >=(greater than or equal to), ==(equal to), !=(not equal to).

Assume variable A is 5 and variable B is 10 -

Operator Description Example
< The operator is used to check that the value of the left operand is less than the value of the right operand, if yes then condition becomes true. A < B result true
> ">" The operator is used to check that the value of the left operand is greater than the value of the right operand, if yes then condition becomes true. A > B result false
<= "<=" The operator is used to check that the value of the left operand is less than or equal to the value of the right operand, if yes then condition becomes true. A <= B result true.
>= ">=" The operator is used to check that the value of the left operand is greater than or equal to the value of the right operand, if yes then condition becomes true. A >= B result false
== "==" The operator is used to check that the value of the left operand is equal to the value of the right operand, if yes then condition becomes true. A == B result false
!= "!=" The operator is used to check that the value of the left operand is not equal to the value of the right operand, if yes then condition becomes true. A != B result true

Logical Operators

C++ logical operators are used to perform logical operations on the given operands. There are three basic logical operator : Logical OR (||), Logical AND (&&), Logical NOT (!).

Assume variable A is 0 and variable B is 1 -

Operator Description Example
&& "&&" If the value of both operands is non-zero, the condition evaluates to true. A && B result false
|| "||" If the value of any operands is non-zero, the condition evaluates to true. A || B result true
! "!" If the condition evaluates to false, the not logical operator will reverse the state and return true. !(A && B) result true.

Bitwise Operators

C++ bitwise operators are used to perform bitwise operations on bit patterns. There are six basic bitwise operator :& (bitwise AND), | (bitwise OR), ^ (bitwise XOR), << (left shift), ~ (bitwise NOT), >> (right shift).

The truth tables for &, |, and ^ are as follows −

P Q P & Q P | Q P ^ Q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1


Consider x=40 and y=80. Binary form of these values are given below.

x = 00101000
y= 01010000

All bit wise operations for x and y are given below.

  x&y = 00000000 (binary) = 0 (decimal)

  x|y = 01111000 (binary) = 120 (decimal)

  ~x = -41 (decimal)

  x^y = 01111000 (binary) = 120 (decimal)

  x << 1 = 01010000 (binary) = 80 (decimal)

  x >> 1 = 00010100 (binary) = 20 (decimal)

Assume variable A holds 60 and variable B holds 13, then −

Operator Description Example
& "&" The output of the bitwise is 1 and if the corresponding bits of the two operands are 1. (A & B) results 12 which is 0000 1100
| "|" The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. (A | B) results 61 which is 0011 1101
^ "^" The result of bitwise XOR operator is 1 if the corresponding bits of two operands are opposite. (A ^ B) results 49 which is 0011 0001
~ "&" Bitwise compliment operator is an unary operator. It changes 1 to 0 and 0 to 1. (A & B) results -61 which is 1100 0011
>> ">>" Right shift operator shifts all bits towards right by certain number of specified bits. (A >> 2) results 15 which is 0000 1111
<< "<<" Left shift operator shifts all bits towards left by certain number of specified bits. (A << B) results 240 which is 1111 0000

Assignment Operators

C++ logical operators are used to assign a new value to a operand.

Operator Description Example
= "=" The operator is used to assigns values from right operands to left operand. C = A + B assigns value "A+B" to C.
+= "+=" The operator is used to add values ​​from the right operand and left operand, then assign the value to the left operand. B += A is equivalent to B = B + A
-= "-=" The operator is used to subtract values ​​from the right operand and left operand, then assign the value to the left operand. B -= A is equivalent to B = B - A
*= "*=" The operator is used to multiple values ​​from the right operand and left operand, then assign the value to the left operand. B *= A is equivalent to B = B * A
/= "/=" The operator is used to divide values ​​from the right operand and left operand, then assign the value to the left operand. B /= A is equivalent to B = B / A
%= "%=" The operator is used to modules the values ​​from the right operand and left operand, then assign the value to the left operand. B %= A is equivalent to B = B % A
<<= " <<=" Left shift AND assignment operator. A <<= 2 is same as A = A << 2
>>= " >>=" Right shift AND assignment operator. A >>= 2 is same as A = A >> 2
&= "&=" Bitwise AND assignment operator. B &= 2 is equivalent to B = B & 2
^= "^=" Bitwise exclusive OR and assignment operator. B ^= 2 is equivalent to B = B ^ 2
|= "|=" Bitwise inclusive OR and assignment operator. B |= 2 is equivalent to B = B | 2

Misc Operators

Here is a list of some miscellaneous operators supported by C++.

Operator Description
, The comma operator will evaluate the first operand and then discard the result, further it will evaluate the second operand and return the result.
sizeof This is the operator used to calculate the size of the variable. For example, sizeof(x), where ‘x’ is float, and will return 4.
Condition ? X : Y If the condition is true then the output will be X otherwise Y.

Exercise:-

1. What is this operator called ?: ?

A. conditional
B. relational
C. casting operator
D. None of the above

View Answer


2. Which operator is having the highest precedence?

A. postfix
B. unary
C. shift
D. equality

View Answer



Program :-

Find total Number of bits required to represent a number in binary in C++

#include<bits/stdc++.h>
using namespace std;

int countBits(int n)
{
	int count=0;
	// While loop will run until we get n = 0
	while(n)
	{
		count++;
		// We are shifting n to right by 1 
		// place as explained above
		n=n>>1;
	}
	return count;
}


int main()
{
	int n;
	cout << "Enter any Number\n";
	cin >> n;
	int count=countBits(n);
	cout << "Number of bits : " << count ;
	return 0;
}


Output :

Enter any Number
45
Number of bits : 6





Visit :


Discussion


* You must be logged in to add comment.