Operators in C++

In C++, operators are symbols used to perform operations on data. There are various types of operators in C++, and they serve different purposes.

Arithmetic Operators

Arithmetic operators are used for basic mathematical operations.

int a = 10, b = 5; int sum = a + b; // Addition int difference = a - b; // Subtraction int product = a * b; // Multiplication int quotient = a / b; // Division int remainder = a % b; // Modulus (remainder)

Comparison Operators

Comparison operators are used to compare two values.

int x = 10, y = 20; bool isEqual = (x == y); // Equal to bool isNotEqual = (x != y); // Not equal to bool isGreater = (x > y); // Greater than bool isLess = (x < y); // Less than bool isGreaterOrEqual = (x >= y); // Greater than or equal to bool isLessOrEqual = (x <= y); // Less than or equal to

Logical Operators

Logical operators are used to perform logical operations.

bool isSunny = true, isWarm = false; bool isNiceDay = isSunny && isWarm; // Logical AND bool isGoodWeather = isSunny isWarm; // Logical OR bool isNotRaining = !isSunny; // Logical NOT

Assignment Operators

Assignment operators are used to assign values to variables.

int num = 10; num += 5; // Equivalent to num = num + 5 num -= 2; // Equivalent to num = num - 2 num *= 3; // Equivalent to num = num * 3 num /= 2; // Equivalent to num = num / 2

Increment and Decrement Operators

Increment (++) and decrement (--) operators are used to increase or decrease a variable by one.

int count = 5; count++; // Increment by 1 count--; // Decrement by 1

Bitwise Operators

Bitwise operators are used to manipulate individual bits in binary representations of numbers.

int a = 5, b = 3; int bitwiseAnd = a & b; // Bitwise AND int bitwiseOr = a b; // Bitwise OR int bitwiseXor = a ^ b; // Bitwise XOR int bitwiseNotA = ~a; // Bitwise NOT int leftShift = a << 2; // Left shift (a << 2 is equivalent to a * 4) int rightShift = a >> 1; // Right shift (a >> 1 is equivalent to a / 2)

Conclusion

C++ operators are symbols used to perform a wide range of operations on data. They include arithmetic operators for mathematical calculations, comparison operators for comparing values, logical operators for boolean logic, assignment operators for variable assignment, and bitwise operators for manipulating individual bits. Understanding and using these operators is essential for performing diverse operations in C++ programming.