# Operators in C++

An operator is a symbol.

It is used to tells the compiler to perform specific mathematical or logical functions.

C++ language have a following types of built-in operators:−

• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators

## Arithmetic Operators

C++ language have a following arithmetic operators.

Assume variable i holds 100 and variable j holds 200 then –

Operator Description Example
+ Adds two operands. i + j = 300
Subtracts second operand from the first. i − j = -100
* Multiplies two operands. i * j = 20000
/ Divide. j / i = 2
% Modulus Operator returns the remainder of integer division. j % i = 0
++ Increment operator increases the value of variable by one. i++ = 101
Decrement operator decreases the value of variable by one. i– = 99

Example : Write a C++ program to demonstrate the various arithmetic operators.

Output

Increment and Decrement Operator

1. Increment operator      ++
2. Decrement operator    —

In a C++ language, Increment operator (++)  increases the value of variable by one and Decrement operator  (–) decreases the value of variable by one.

There are two form of increment  and decrement operator:

1. Prefix form
2. Postfix form

Prefix Form : In the prefix expression operator appears in the expression before the operands.

Example : ++A

In the prefix form first the value of operand is increment or decrement than the value of operand is used in expression.

For Example: int  i = 10;
j = ++i ;    // j=11 , i=11

Postfix Form: In the postfix expression operator appears in the expression after the operands.

Example : A++

In the postfix form first the value of operand is used in expression than value of operand is incremented or decremented.

For Example: int  i = 10;
j = i++ ;    // j=10, i=11

Example: Write a C++ program to demonstrate the increment and decrement operator.

Output

## Relational Operators

C++ Language has a following relational operators.

For example: Assume variable A holds 100 and variable B holds 200 then −

 Operator Description Example == Equals to (A == B) is not true. != Not equal (A != B) is true. > Greater than (A > B) is not true. < Less than (A < B) is true. >= Greater than or equals to (A >= B) is not true. <= Less than or equals to (A <= B) is true.

Example:  Write a C++ Program to find the largest of two numbers.

Output

## Logical Operators

There are three basic logical operators available in C++ language.

Assume variable A and B holds 1 and 0 respectively then –

 Operator Description Example && AND operator (the condition becomes true if both the operands are non-zero) (A && B) is false. || OR Operator (the condition becomes true if any of the two operands is non-zero) (A || B) is true. ! NOT Operator (this operator reverse the logical state of operand. For example: If condition is true, then NOT operator will make it false) !(A && B) is true.

Example:  Write a C++ Program to find the largest among three numbers using AND logical operator.

Output

## Bitwise Operators

In a C++ programming language Bitwise operator works on bits and perform bit-by-bit operation.

There are following bitwise operators in C++:-

 Operator Description & Binary AND Operator | Binary OR Operator ^ Binary XOR Operator ~ Binary One’s Complement Operator is unary << Binary Left Shift Operator >> Binary Right Shift Operator

The truth tables for &, |, and ^ is as follows –

a b a & b a | b a ^ b
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

Assume A = 60 and B = 13 in binary format, they will be as follows −

A in binary     = 0011 1100         (60)

B in binary     = 0000 1101          (13)

—————–

Bitwise AND (&)

A      = 0011 1100         (60)

&         B      = 0000 1101          (13)

———————————————————

0000 1100           (12)

Bitwise OR (|)

A      = 0011 1100         (60)

|          B      = 0000 1101          (13)

———————————————————

0011 1101           (61)

Bitwise XOR (^)

A      = 0011 1100         (60)

^         B      = 0000 1101          (13)

———————————————————

0011 0001           (49)

Binary Left Shift Operator

Left shift operator shift all the bits in a left direction to specified number of times.

Exapmle:

Output:

Output

Exapmle:  Write a program to demonstrate Bitwise operator.

Output

## Assignment Operators

In a C++  language there are following assignment operators  −

Operator Description
= assignment operator (it assigns values of right side operands to left side operand )
-= Subtract assignment operator
*= Multiply  assignment operator
/= Divide assignment operator
%= Modulus assignment operator
<<= Left shift assignment operator
>>= Right shift assignment operator
&= Bitwise AND assignment operator
^= Bitwise exclusive OR assignment operator
|= Bitwise inclusive OR assignment operator

Example:

Output

Subtract Assignment Operator

Example:

Output

Multiply  assignment operator

Example:

Output

The ?  Operator

This operator also known as conditional operator . It is similar to if-else statement.

Syntax of ? operator

Expression1 ? Expression 2 : Expression 3;

There are three expressions in ? operator, Expression1, Expression2, and Expression3 .

The value of a ? expression is determined like this −

• If Exp1 is true, then Exp2 is evaluated.
• If Exp1 is false, then Exp3 is evaluated.

Example: write a program to find the grater between two number using ? operator.

Output:

Example: write a program to find the grater among three number using ? operator.

Output

Categories C++
{"ft_lang_label":"__label__en","ft_lang_prob":0.6533589,"math_prob":0.9862524,"size":8968,"snap":"2022-27-2022-33","text_gpt3_token_len":3023,"char_repetition_ratio":0.16521642,"word_repetition_ratio":0.25755167,"special_character_ratio":0.41257805,"punctuation_ratio":0.12934783,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9987221,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-12T14:12:49Z\",\"WARC-Record-ID\":\"<urn:uuid:5c8b45f0-5206-4643-aafc-3d8813398920>\",\"Content-Length\":\"193437\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b80c294a-27fe-4ccd-baa8-323f3f3da394>\",\"WARC-Concurrent-To\":\"<urn:uuid:ae157077-c86a-4036-8a7c-b945595cf35e>\",\"WARC-IP-Address\":\"172.67.149.171\",\"WARC-Target-URI\":\"https://www.ebhor.com/operators-in-cpp/\",\"WARC-Payload-Digest\":\"sha1:INQE5XF27SZO7VX4EDWXY3XXREGYQRZ6\",\"WARC-Block-Digest\":\"sha1:USYV4KZB3LAXEG6VXEC4HGFQANOYFJJW\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882571719.48_warc_CC-MAIN-20220812140019-20220812170019-00097.warc.gz\"}"}