C++ Operators


C++ Operators

In this tutorial, we will learn about operators in C++. We will cover various categories of operators and their usage.


Arithmetic Operators

Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division.

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulusa % b
++Incrementa++
--Decrementa--

Assignment Operators

Assignment operators are used to assign values to variables. They combine an arithmetic operation with assignment.

OperatorDescriptionExampleSame as
=Assignmenta = ba = b
+=Add and assigna += ba = a + b
-=Subtract and assigna -= ba = a - b
*=Multiply and assigna *= ba = a * b
/=Divide and assigna /= ba = a / b
%=Modulus and assigna %= ba = a % b
&=Bitwise AND and assigna &= ba = a & b
|=Bitwise OR and assigna |= ba = a | b
^=Bitwise XOR and assigna ^= ba = a ^ b
<<=Left shift and assigna <<= ba = a << b
>>=Right shift and assigna >>= ba = a >> b

Comparison Operators

Comparison operators are used to compare two values. They return a boolean result: true or false.

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b

Logical Operators

Logical operators are used to perform logical operations on boolean values. They are commonly used in conditional statements.

OperatorDescriptionExample
&&Logical ANDa && b
||Logical ORa || b
!Logical NOT!a

Bitwise Operators

Bitwise operators are used to perform bit-level operations on integer types. These operations are faster and closer to the hardware.

OperatorDescriptionExample
&Bitwise ANDa & b
|Bitwise ORa | b
^Bitwise XORa ^ b
~Bitwise NOT~a
<<Left shifta << b
>>Right shifta >> b


Arithmetic Operators Example

  1. Declare two integer variables a and b.
  2. Assign values to a and b.
  3. Perform addition, subtraction, multiplication, division, and modulus operations using a and b.
  4. Print the results of each operation.

C++ Program

#include <iostream>
using namespace std;
int main() {
    int a = 10, b = 5;
    int add = a + b;
    int subtract = a - b;
    int multiply = a * b;
    int divide = a / b;
    int modulus = a % b;

    cout << "Addition result: " << add << endl;
    cout << "Subtraction result: " << subtract << endl;
    cout << "Multiplication result: " << multiply << endl;
    cout << "Division result: " << divide << endl;
    cout << "Modulus result: " << modulus << endl;

    return 0;
}

Output

Addition result: 15
Subtraction result: 5
Multiplication result: 50
Division result: 2
Modulus result: 0


Assignment Operators Example

  1. Declare an integer variable num and assign a value.
  2. Reset the value of num before each assignment operation.
  3. Use assignment operators to perform various operations on num.
  4. Print the result of each operation.

C++ Program

#include <iostream>
using namespace std;
int main() {
    int num = 10;
    cout << "Initial value of num: " << num << endl;

    num += 5; // Addition assignment
    cout << "After addition, num: " << num << endl;

    num = 10; // Reset num
    num -= 3; // Subtraction assignment
    cout << "After subtraction, num: " << num << endl;

    num = 10; // Reset num
    num *= 2; // Multiplication assignment
    cout << "After multiplication, num: " << num << endl;

    num = 10; // Reset num
    num /= 4; // Division assignment
    cout << "After division, num: " << num << endl;

    num = 10; // Reset num
    num %= 3; // Modulus assignment
    cout << "After modulus, num: " << num << endl;

    num = 10; // Reset num
    num &= 3; // Bitwise AND assignment
    cout << "After bitwise AND, num: " << num << endl;

    num = 10; // Reset num
    num |= 3; // Bitwise OR assignment
    cout << "After bitwise OR, num: " << num << endl;

    num = 10; // Reset num
    num ^= 3; // Bitwise XOR assignment
    cout << "After bitwise XOR, num: " << num << endl;

    num = 10; // Reset num
    num <<= 2; // Left shift assignment
    cout << "After left shift, num: " << num << endl;

    num = 10; // Reset num
    num >>= 1; // Right shift assignment
    cout << "After right shift, num: " << num << endl;

    return 0;
}

Output

Initial value of num: 10
After addition, num: 15
After subtraction, num: 7
After multiplication, num: 20
After division, num: 2
After modulus, num: 1
After bitwise AND, num: 2
After bitwise OR, num: 11
After bitwise XOR, num: 9
After left shift, num: 40
After right shift, num: 5


Comparison Operators Example

  1. Declare two integer variables x and y.
  2. Compare the values of x and y using comparison operators.
  3. Print the comparison result.

C++ Program

#include <iostream>
using namespace std;
int main() {
    int x = 5, y = 10;

    cout << "x > y: " << (x > y) << endl; // Outputs 0 since x is not greater than y
    cout << "x < y: " << (x < y) << endl; // Outputs 1 since x is less than y
    cout << "x >= y: " << (x >= y) << endl; // Outputs 0 since x is not greater than or equal to y
    cout << "x <= y: " << (x <= y) << endl; // Outputs 1 since x is less than or equal to y
    cout << "x == y: " << (x == y) << endl; // Outputs 0 since x is not equal to y
    cout << "x != y: " << (x != y) << endl; // Outputs 1 since x is not equal to y

    return 0;
}

Output

x > y: 0
x < y: 1
x >= y: 0
x <= y: 1
x == y: 0
x != y: 1


Logical Operators Example

  1. Declare two boolean variables a and b.
  2. Perform logical operations using logical operators.
  3. Print the result of each operation.

C++ Program

#include <iostream>
using namespace std;
int main() {
    bool a = true, b = false;

    cout << "a && b: " << (a && b) << endl; // Outputs 0 (false)
    cout << "a || b: " << (a || b) << endl; // Outputs 1 (true)
    cout << "!a: " << (!a) << endl;    // Outputs 0 (false)

    return 0;
}

Output

a && b: 0
a || b: 1
!a: 0


Bitwise Operators Example

  1. Declare two integer variables a and b.
  2. Perform bitwise operations using bitwise operators.
  3. Print the result of each operation.

C++ Program

#include <iostream>
using namespace std;
int main() {
    int a = 5, b = 3;

    cout << "a & b: " << (a & b) << endl;  // Outputs 1
    cout << "a | b: " << (a | b) << endl;  // Outputs 7
    cout << "a ^ b: " << (a ^ b) << endl;  // Outputs 6
    cout << "~a: " << (~a) << endl;     // Outputs -6
    cout << "a << 1: " << (a << 1) << endl; // Outputs 10
    cout << "b >> 1: " << (b >> 1) << endl; // Outputs 1

    return 0;
}

Output

a & b: 1
a | b: 7
a ^ b: 6
~a: -6
a << 1: 10
b >> 1: 1