cpp
operators
01_operators.cpp⚙️cpp
/**
* ============================================================
* C++ OPERATORS - COMPLETE GUIDE
* ============================================================
*
* This file covers ALL C++ operators:
* - Arithmetic operators
* - Relational operators
* - Logical operators
* - Bitwise operators
* - Assignment operators
* - Other operators
*
* Compile: g++ -std=c++17 -Wall 01_operators.cpp -o operators
* Run: ./operators
*
* ============================================================
*/
#include <iostream>
#include <iomanip>
#include <cmath>
#include <bitset>
using namespace std;
int main() {
cout << "============================================" << endl;
cout << " C++ OPERATORS COMPLETE GUIDE" << endl;
cout << "============================================" << endl << endl;
// ========================================================
// PART 1: ARITHMETIC OPERATORS
// ========================================================
cout << "--- PART 1: ARITHMETIC OPERATORS ---" << endl << endl;
int a = 20, b = 7;
cout << "a = " << a << ", b = " << b << endl << endl;
// Basic arithmetic
cout << "Addition: a + b = " << (a + b) << endl;
cout << "Subtraction: a - b = " << (a - b) << endl;
cout << "Multiplication: a * b = " << (a * b) << endl;
cout << "Division: a / b = " << (a / b) << " (integer division)" << endl;
cout << "Modulus: a % b = " << (a % b) << " (remainder)" << endl;
// Division with doubles
double x = 20.0, y = 7.0;
cout << "\nWith doubles:" << endl;
cout << "20.0 / 7.0 = " << (x / y) << endl;
// Unary operators
int num = 5;
cout << "\nUnary operators (num = 5):" << endl;
cout << "+num = " << (+num) << endl;
cout << "-num = " << (-num) << endl;
// Increment and decrement
int counter = 10;
cout << "\nIncrement/Decrement (counter = 10):" << endl;
cout << "counter++ = " << counter++ << " (post-increment, now counter = " << counter << ")" << endl;
cout << "++counter = " << ++counter << " (pre-increment)" << endl;
cout << "counter-- = " << counter-- << " (post-decrement, now counter = " << counter << ")" << endl;
cout << "--counter = " << --counter << " (pre-decrement)" << endl;
cout << endl;
// ========================================================
// PART 2: RELATIONAL (COMPARISON) OPERATORS
// ========================================================
cout << "--- PART 2: RELATIONAL OPERATORS ---" << endl << endl;
int p = 10, q = 20;
cout << "p = " << p << ", q = " << q << endl << endl;
cout << boolalpha; // Print true/false instead of 1/0
cout << "Equal: p == q : " << (p == q) << endl;
cout << "Not equal: p != q : " << (p != q) << endl;
cout << "Greater than: p > q : " << (p > q) << endl;
cout << "Less than: p < q : " << (p < q) << endl;
cout << "Greater or equal: p >= q : " << (p >= q) << endl;
cout << "Less or equal: p <= q : " << (p <= q) << endl;
// Three-way comparison (C++20)
#if __cplusplus >= 202002L
cout << "\nThree-way comparison (p <=> q):" << endl;
auto result = p <=> q;
if (result < 0) cout << "p is less than q" << endl;
else if (result > 0) cout << "p is greater than q" << endl;
else cout << "p equals q" << endl;
#endif
cout << noboolalpha << endl;
// ========================================================
// PART 3: LOGICAL OPERATORS
// ========================================================
cout << "--- PART 3: LOGICAL OPERATORS ---" << endl << endl;
bool t = true, f = false;
cout << boolalpha;
cout << "t = true, f = false" << endl << endl;
cout << "AND (&&):" << endl;
cout << " t && t = " << (t && t) << endl;
cout << " t && f = " << (t && f) << endl;
cout << " f && t = " << (f && t) << endl;
cout << " f && f = " << (f && f) << endl;
cout << "\nOR (||):" << endl;
cout << " t || t = " << (t || t) << endl;
cout << " t || f = " << (t || f) << endl;
cout << " f || t = " << (f || t) << endl;
cout << " f || f = " << (f || f) << endl;
cout << "\nNOT (!):" << endl;
cout << " !t = " << (!t) << endl;
cout << " !f = " << (!f) << endl;
// Short-circuit evaluation
cout << "\nShort-circuit evaluation:" << endl;
int val = 5;
// Second condition won't be evaluated if first is false
if (false && (++val > 0)) { }
cout << "false && (++val): val = " << val << " (not incremented)" << endl;
// Second condition won't be evaluated if first is true
if (true || (++val > 0)) { }
cout << "true || (++val): val = " << val << " (not incremented)" << endl;
cout << noboolalpha << endl;
// ========================================================
// PART 4: BITWISE OPERATORS
// ========================================================
cout << "--- PART 4: BITWISE OPERATORS ---" << endl << endl;
unsigned int m = 60; // 0011 1100
unsigned int n = 13; // 0000 1101
cout << "m = " << m << " (binary: " << bitset<8>(m) << ")" << endl;
cout << "n = " << n << " (binary: " << bitset<8>(n) << ")" << endl << endl;
cout << "Bitwise AND (&):" << endl;
cout << " m & n = " << (m & n) << " (binary: " << bitset<8>(m & n) << ")" << endl;
cout << "\nBitwise OR (|):" << endl;
cout << " m | n = " << (m | n) << " (binary: " << bitset<8>(m | n) << ")" << endl;
cout << "\nBitwise XOR (^):" << endl;
cout << " m ^ n = " << (m ^ n) << " (binary: " << bitset<8>(m ^ n) << ")" << endl;
cout << "\nBitwise NOT (~):" << endl;
cout << " ~m = " << (~m) << " (binary: " << bitset<32>(~m) << ")" << endl;
cout << "\nLeft shift (<<):" << endl;
cout << " m << 2 = " << (m << 2) << " (binary: " << bitset<8>(m << 2) << ")" << endl;
cout << "\nRight shift (>>):" << endl;
cout << " m >> 2 = " << (m >> 2) << " (binary: " << bitset<8>(m >> 2) << ")" << endl;
// Practical bitwise operations
cout << "\nPractical uses:" << endl;
// Check if number is even/odd
int testNum = 7;
cout << testNum << " is " << ((testNum & 1) ? "odd" : "even") << endl;
// Multiply/divide by powers of 2
cout << "10 << 1 = " << (10 << 1) << " (multiply by 2)" << endl;
cout << "10 >> 1 = " << (10 >> 1) << " (divide by 2)" << endl;
// Swap without temp variable
int swap1 = 5, swap2 = 10;
cout << "\nSwap using XOR:" << endl;
cout << "Before: swap1=" << swap1 << ", swap2=" << swap2 << endl;
swap1 ^= swap2;
swap2 ^= swap1;
swap1 ^= swap2;
cout << "After: swap1=" << swap1 << ", swap2=" << swap2 << endl;
cout << endl;
// ========================================================
// PART 5: ASSIGNMENT OPERATORS
// ========================================================
cout << "--- PART 5: ASSIGNMENT OPERATORS ---" << endl << endl;
int v = 10;
cout << "Starting with v = " << v << endl << endl;
v = 10; // Reset
cout << "v = 10; v is now " << v << endl;
v += 5; // Same as v = v + 5
cout << "v += 5; v is now " << v << endl;
v -= 3; // Same as v = v - 3
cout << "v -= 3; v is now " << v << endl;
v *= 2; // Same as v = v * 2
cout << "v *= 2; v is now " << v << endl;
v /= 3; // Same as v = v / 3
cout << "v /= 3; v is now " << v << endl;
v %= 3; // Same as v = v % 3
cout << "v %= 3; v is now " << v << endl;
// Bitwise assignment
v = 60;
cout << "\nBitwise assignments (v = 60 = " << bitset<8>(60) << "):" << endl;
v &= 13;
cout << "v &= 13; v is now " << v << " (" << bitset<8>(v) << ")" << endl;
v = 60;
v |= 13;
cout << "v |= 13; v is now " << v << " (" << bitset<8>(v) << ")" << endl;
v = 60;
v ^= 13;
cout << "v ^= 13; v is now " << v << " (" << bitset<8>(v) << ")" << endl;
v = 60;
v <<= 2;
cout << "v <<= 2; v is now " << v << " (" << bitset<8>(v) << ")" << endl;
v = 60;
v >>= 2;
cout << "v >>= 2; v is now " << v << " (" << bitset<8>(v) << ")" << endl;
cout << endl;
// ========================================================
// PART 6: OTHER OPERATORS
// ========================================================
cout << "--- PART 6: OTHER OPERATORS ---" << endl << endl;
// Ternary operator (conditional)
int age = 20;
string status = (age >= 18) ? "adult" : "minor";
cout << "Ternary operator:" << endl;
cout << " age = " << age << ", status = " << status << endl;
// Comma operator
int r, s;
r = (s = 3, s + 2); // s=3, then r = 3+2 = 5
cout << "\nComma operator:" << endl;
cout << " r = (s = 3, s + 2); r = " << r << ", s = " << s << endl;
// sizeof operator
cout << "\nsizeof operator:" << endl;
cout << " sizeof(int) = " << sizeof(int) << " bytes" << endl;
cout << " sizeof(double) = " << sizeof(double) << " bytes" << endl;
int arr[] = {1, 2, 3, 4, 5};
cout << " sizeof(arr) = " << sizeof(arr) << " bytes" << endl;
cout << " Array elements = " << sizeof(arr)/sizeof(arr[0]) << endl;
// Address and dereference operators
int value = 42;
int* ptr = &value; // & gets address
cout << "\nAddress/Dereference operators:" << endl;
cout << " value = " << value << endl;
cout << " &value = " << &value << endl;
cout << " ptr = " << ptr << endl;
cout << " *ptr = " << *ptr << endl;
// Member access operators
// . for objects, -> for pointers (covered in OOP section)
// Scope resolution operator ::
cout << "\nScope resolution operator (::):" << endl;
cout << " std::cout is cout from std namespace" << endl;
cout << endl;
// ========================================================
// PART 7: OPERATOR PRECEDENCE
// ========================================================
cout << "--- PART 7: OPERATOR PRECEDENCE ---" << endl << endl;
cout << "Precedence (highest to lowest):" << endl;
cout << "1. () [] -> . :: (Scope/member)" << endl;
cout << "2. ++ -- ! ~ + - * & sizeof (Unary)" << endl;
cout << "3. * / % (Multiplicative)" << endl;
cout << "4. + - (Additive)" << endl;
cout << "5. << >> (Shift)" << endl;
cout << "6. < <= > >= (Relational)" << endl;
cout << "7. == != (Equality)" << endl;
cout << "8. & (Bitwise AND)" << endl;
cout << "9. ^ (Bitwise XOR)" << endl;
cout << "10. | (Bitwise OR)" << endl;
cout << "11. && (Logical AND)" << endl;
cout << "12. || (Logical OR)" << endl;
cout << "13. ?: (Ternary)" << endl;
cout << "14. = += -= *= etc. (Assignment)" << endl;
cout << "15. , (Comma)" << endl;
// Examples
cout << "\nPrecedence examples:" << endl;
cout << "2 + 3 * 4 = " << (2 + 3 * 4) << " (not 20)" << endl;
cout << "(2 + 3) * 4 = " << ((2 + 3) * 4) << endl;
cout << "5 > 3 && 2 < 4 = " << boolalpha << (5 > 3 && 2 < 4) << noboolalpha << endl;
// Tip: When in doubt, use parentheses!
cout << "\n💡 TIP: Use parentheses for clarity!" << endl;
cout << endl;
cout << "============================================" << endl;
cout << "Program completed!" << endl;
cout << "============================================" << endl;
return 0;
}
// ============================================================
// EXERCISES:
// ============================================================
/*
* 1. Write expressions to:
* - Check if a number is between 10 and 100
* - Check if a year is a leap year
* - Find the last digit of a number
*
* 2. Use bitwise operators to:
* - Set the 3rd bit of a number
* - Clear the 5th bit of a number
* - Toggle the 2nd bit of a number
*
* 3. Write a program that uses the ternary operator
* to find the maximum of three numbers
*
* 4. Create expressions that demonstrate operator
* precedence issues (and fix them with parentheses)
*/