0% found this document useful (0 votes)
55 views

C Plus Plus Operators

C++ has various types of operators for performing arithmetic, logical, relational, bitwise, and assignment operations on variables and values. These include operators for basic math functions, comparing values, manipulating bits, incrementing/decrementing, and combining operations with assignments. C++ operator precedence determines the order that operations are performed, with some like multiplication having higher precedence than others like addition.

Uploaded by

Jonna Lynne
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

C Plus Plus Operators

C++ has various types of operators for performing arithmetic, logical, relational, bitwise, and assignment operations on variables and values. These include operators for basic math functions, comparing values, manipulating bits, incrementing/decrementing, and combining operations with assignments. C++ operator precedence determines the order that operations are performed, with some like multiplication having higher precedence than others like addition.

Uploaded by

Jonna Lynne
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

C++ operator is a symbol that is used to perform mathematical or logical manipulations.

C++ language
is rich with built-in operators.

Arithmetic Operators Increment and Decrement


Operator Description Operators
+ Addition Operator Description
– Subtraction ++ Increment
* Multiplication −− Decrement
/ Division
% Modulus
This type of operators are discussed in the article C++ Variable Types. Basic arithmetic operators are +, -, *,
/, %. In addition to these operators there are 2 operators to increase and decrease value of the variable. The
increment operator is ++ and the decrement operator is --. These operators can be used as before the variable
(prefix) and after the variable (postfix). For example, you can change the values of the variable in the following way:
int a = 0;
++a; //now "a" is 1
cout << a << endl;
a++; //now "a" is 2
cout << a << endl;
--a; //decrement a. it's 1 now
cout << a << endl;
--a; //decrement a; it is 0 now
cout << a;
cin.ignore();

The output of the program should be the following


1
2
1
0

Below code will explain you that pre-increment first increments the value then assigns the value to the variable. Post
increment first assigns the value then increments it.

a = ++b; // a==4, b==4


a = b++; // a==3, b==4

Below code displays the value of varibles using pre and post increment.
int main()
{
int i = 0;
while(i < 10)
{
cout << i++; //post increment
}
cout << endl;
i = 0;
while(i<10)
{
cout << ++i; //pre increment
}
return 0;
}
Below is the output of above program
0123456789
12345678910

Relational Operators
This category of the operators is used to compare different values. The result of the operation is a Boolean value
(true or false). The relational operators are used in the following form Operand1 operatorOperand2, where
operands can be some variables or literals that are compared.

Operator Description
== Is equal to (Returns true if the operands are equal. Otherwise false.)
!= Is not equal to (Returns true if the operands are not equal. Otherwise
false.)
> Greater than (Returns true if the operand1 is greater than operand2.
Otherwise false.)
< Less than (Returns true if the operand1 is less than operand2. Otherwise
false.)
>= Greater than or equal to (Returns true if the operand1 is greater than
operand2 or equal to operand 2. Otherwise false.)
<= Less than or equal to (Returns true if the operand1 is less than operand2
or equal to operand 2. Otherwise false.)

Now we can try these operators. Remember, that "cout" outputs 1 for true and 0 for false:
int three = 3;
int five = 5;
cout << " 3 is equal to 5 = " << (three == five) << endl;
cout << " 3 is not equal to 5 = " << (three != five) << endl;
cout << " 3 is less than 5 = " << (three < five) << endl;
cout << " 3 is greater than 5 = " << (three > five) << endl;
cout << " 3 is not less than 5 = " << (three >= five) << endl;
cout << " 3 is not greater than 5 = " << (three <= five) << endl;

The output for above code is:


3 is equal to 5 = 0
3 is not equal to 5 = 1
3 is less than 5 = 1
3 is greater than 5 = 0
3 is not less than 5 = 0
3 is not greater than 5 = 1

Logical Operators
Operator Description
&& And operator. Performs a logical conjunction on two expressions.
(if both expressions evaluate to True, result is True. If either expression evaluates to
False, result is False)
|| Or operator. Performs a logical disjunction on two expressions.
(if either or both expressions evaluate to True, result is True)
! Not operator. Performs logical negation on an expression.

C++ provides 3 operations from Boolean algebra:


and (conjunction) operator &&
or (disjunction) operator ||
not (negation) operator !
The result of Boolean operations for 2 variables x and y is presented in the following tables:

Here is an example of the use of Boolean operators:


//initialize x with true
bool x = true;
//initialize y with false
bool y = false;
//now x is x AND y (false)
x = x&&y;

Bitwise Operators
Bitwise operators are similar to the logic operators, but they perform the same logical operations on bits. All data in
memory is represented in the binary form. So, variables in form of bits contains only 0 or 1.

Operator Description
<< Binary Left Shift Operator
>> Binary Right Shift Operator
~ Binary Ones Complement
Operator
& Binary AND Operator
^ Binary XOR Operator
| Binary OR Operator

C++ provides possibility to use the following bitwise operators:

Binary AND operator "&"

The resultant bit is set to 1 if and only if both variables have 1 in the corresponding bit. Example of binary &:

10100110 & 00101010 = 00100010


Binary OR operator "|"

The resultant bit is set to 1 if at least one of the variables has 1 in the corresponding bit. Example of binary |:

10100110 | 00101010 = 10101110

Binary XOR operator "^"

The result bit is set to 1 if only one of the variables has 1 in the corresponding bit. Example of Binary xor:

10100110 ^ 00101010 = 10001100

Binary NOT operator "~"

Flips the bits of the variable. For Example:

~10100110 = 01011001

Binary Left Shift Operator "<< N"


Will shift 'N' number of bits to the left. In simple words, N number of bits from the Left will be removed and N number
of Zeros will be added to the Right. For Example:

10100110 << 3 = 00110000

Binary Right Shift Operator ">> N"


Will shift 'N' number of bits to the right. In simple words, N number of bits from the Right will be removed and N
number of Zeros will be added to the Left. For Example:

10100110 >> 2 = 00101001


Assignment Operators
The operator "=" is an assignment operator in C++ and it assigns a value to the objects on the left. C++ provides
capability to combine assignment operator with almost all the operation we have discussed above and forms a
"Composite Assignment Operator".

Operator Description
= Assign
+= Increments, then assigns
-= Decrements, then assigns
*= Multiplies, then assigns
/= Divides, then assigns
%= Modulus, then assigns
<<= Left shift and assigns
>>= Right shift and assigns
&= Bitwise AND assigns
^= Bitwise exclusive OR and assigns
|= Bitwise inclusive OR and assigns

For example we can add a value to a variable as shown below:


int k = 9;

//normal way of adding value


k = k + 7;

//you can add value in a more compact way by combining two operators

//Composite assignment will be

k += 7; // this is equal to k = k + 7

Composite assignment can be represented in a form:


Expression1 composite-assignment-operator Expression2

where composite-assignment-operator can be one of the following.

Let X = 10 initially for the below examples

Composite
Assignment Example Is Equivalent to Output
Operator
+= X += 2 X=X+2 12
-= X -= 2 X=X-2 8
*= X *= 2 X=X*2 20
/= X /= 2 X=X/2 5
%= X %= 2 X=X%2 0
<<= X <<= 2 X = X << 2 40
>>= X >>= 2 X = X >> 2 2
&= X &= 2 X=X&2 2
^= X ^= 2 X=X^2 8
|= X |= 2 X=X|2 10
It is suggested to use Composite Assignment Operator for more efficient code
X +=10; // will be faster than X = X + 10
X = X + 10; // will be slower than X += 10

Misc Operators
Operator Description
, Comma operator
sizeof() Returns the size of an memory location.
& Returns the address of an memory location.
* Pointer to a variable.
?: Conditional Expression

There are some operators that are not included in any categories listed above, but they are useful and can be used
in your programs:

"sizeof" Operator

sizeof operator returns the size of the variable or type. Here "size" means how many bytes of memory is being used
by a variable or type.

cout << sizeof(int) << endl; // will print 4


cout << sizeof(x) << endl; // will print size of x

Coma (",") Operator

Coma "," Operator is used when you need to perform a sequence of operations. You used it when initialized a list of
variable of the same type:

double a = 2.0,
b = 4.3,
c = 2;

Operator Precedence
Operator precedence determines how an expression is evaluated. Some operators will have higher precedence
than others. For example, multiplication operator will have higher precedence than addition operator.

For example a = 2 + 3 * 5; here, a will be assigned 17, not 25 because operator * has higher precedence than +, so
3*5 gets multiplied first and then 2 gets added to the multiplication result of 3*5.
In the table below, operators with the highest precedence appear at the top of the table and operators with the
lowest precedence appear at the bottom. In the expression, higher precedence operators will be evaluated first.

You might also like