Introduction to Operators in CPP

Mastering Operators in C++ Programming

In the realm of C++ programming, operators are symbolic tools that instruct the compiler to execute specific mathematical or logical operations. C++ boasts a rich collection of built-

in operators, spanning a variety of types such as:

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

This comprehensive chapter delves into these operator categories, providing an in-depth exploration of their functionality.

Arithmetic Operators in C++: A Fundamental Toolkit

C++ Arithmetic Operators allow you to perform mathematical computations, such as addition, subtraction, multiplication, and division, on numeric operands.Arithmetic operators are the cornerstone of mathematical manipulation in C++. To illustrate, let’s assume we have two variables, A with a value of 10 and B with a value of 20.

OperatorDescriptionExample
+AdditionA + B = 30
SubtractionA – B = -10
*MultiplicationA * B = 200
/DivisionB / A = 2
%Modulus (remainder of integer division)B % A = 0
++Increment operator (increases by one)A++ = 11
Decrement operator (decreases by one)A– = 9

Relational Operators: Comparing Values

Relational operators enable the comparison of values between variables. Suppose A = 10 and B = 20.

OperatorDescriptionExample
==Equal to(A == B) is false
!=Not equal to(A != B) is true
>Greater than(A > B) is false
<Less than(A < B) is true
>=Greater than or equal to(A >= B) is false
<=Less than or equal to(A <= B) is true

Logical Operators: Making Logical Decisions

Logical operators facilitate logical decision-making processes based on multiple conditions. Given A = 1 and B = 0.

OperatorDescriptionExample
&&Logical AND(A && B) is false
||Logical OR(A || B) is true
!Logical NOT (negation)!(A && B) is true

Bitwise Operators: Manipulating Individual Bits

Bitwise operators operate on individual bits of variables. Consider A = 60 (0011 1100) and B = 13 (0000 1101).

OperatorDescriptionExample
&Bitwise ANDA & B = 12
|Bitwise ORA | B = 61
^Bitwise XOR (exclusive OR)A ^ B = 49
~Bitwise NOT (one’s complement)~A = -61
<<Left shiftA << 2 = 240
>>Right shiftA >> 2 = 15

Assignment Operators: Managing Assignments

Assignment operators play a pivotal role in assigning values to variables and performing operations. Assume C = A + B.

OperatorDescriptionExample
=Simple assignmentC = A + B
+=Add and assignC += A (C = C + A)
-=Subtract and assignC -= A (C = C – A)
*=Multiply and assignC *= A (C = C * A)
/=Divide and assignC /= A (C = C / A)
%=Modulus and assignC %= A (C = C % A)
<<=Left shift and assignC <<= 2 (C = C << 2)
>>=Right shift and assignC >>= 2 (C = C >> 2)
&=Bitwise AND and assignC &= 2 (C = C & 2)
^=Bitwise XOR and assignC ^= 2 (C = C ^ 2)
|=Bitwise OR and assignC |= 2 (C = C | 2)

Miscellaneous Operators: The Special Ones

The remaining operators include:

  • sizeof: Returns the size of a variable.
  • Condition ? X : Y: Conditional operator. Returns X if the condition is true, else Y.
  • Comma: Comma operator sequences operations and returns the last expression.
  • . (dot) and -> (arrow): Member operators for accessing class, structure, and union members.
  • Cast: Casting operators convert one data type to another.
  • &: Returns the address of a variable.
  • *: Pointer operator, creates a pointer to a variable.

Operator Precedence: Ordering Operations

Operator precedence determines the order of operations in an expression. Operators with higher precedence are evaluated first. Here’s a snippet of operator precedence:

Category Operator Associativity 
Postfix () [] -> . ++ – –  Left to right 
Unary + – ! ~ ++ – – (type)* & sizeof Right to left 
Multiplicative  * / % Left to right 
Additive  + – Left to right 
Shift  << >> Left to right 
Relational  < <= > >= Left to right 
Equality  == != Left to right 
Bitwise AND Left to right 
Bitwise XOR Left to right 
Bitwise OR Left to right 
Logical AND && Left to right 
Logical OR || Left to right 
Conditional ?: Right to left 
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left 
Comma ,Left to right 

Understanding operator precedence is crucial for accurate and efficient expression evaluation.

In conclusion, mastering operators is vital in C++ programming. They serve as building blocks for various computations and decisions, making them indispensable tools for crafting robust and efficient code.


Discover more from PiEmbSysTech - Embedded Systems & VLSI Lab

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech - Embedded Systems & VLSI Lab

Subscribe now to keep reading and get access to the full archive.

Continue reading