-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpp1_3.html
More file actions
106 lines (87 loc) · 3.64 KB
/
Copy pathCpp1_3.html
File metadata and controls
106 lines (87 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<article>
<h1>C++ I: Page 3 - Operators & Logic</h1>
<section>
<h2>Manipulating Data with Operators</h2>
<p>C++ provides a rich set of operators for manipulating data, ranging from basic arithmetic to low-level bitwise operations. Understanding operator precedence is critical for complex expressions.</p>
<h3>The Code implementation</h3>
<pre><code class="language-cpp">/*
* File: operators.cpp
* Demonstration of various C++ operator categories
*/
#include <iostream>
#include <bitset>
#include <string>
using namespace std;
// Function to demonstrate precedence
void PrecedenceDemo() {
int a = 2;
int b = 3;
int c = 4;
// Parentheses explicitly force order
int result1 = a + b * c; // 2 + 12 = 14 (Multiplication first)
int result2 = (a + b) * c; // 5 * 4 = 20 (Addition first)
cout << "Precedence 1: " << result1 << endl;
cout << "Precedence 2: " << result2 << endl;
}
int main() {
// 1. Arithmetic Operators
int a = 10, b = 3;
cout << "Arithmetic: " << a + b << ", " << a % b << endl;
// 2. Relational Operators
bool isEqual = (a == b);
bool isGreater = (a > b);
// 3. Logical Operators
bool result = (isGreater && !isEqual); // AND and NOT
// 4. Bitwise Operators
int val1 = 5; // 0101
int val2 = 3; // 0011
int bitAnd = val1 & val2; // 0001
int bitOr = val1 | val2; // 0111
int bitXor = val1 ^ val2; // 0110
cout << "Bitwise AND: " << bitAnd << endl;
// 5. Assignment Operators
a += 5; // Equivalent to a = a + 5
a *= 2;
// 6. Unary Operators
int x = 5;
int y = ++x; // Prefix: Increment x, then assign to y (x=6, y=6)
int z = x++; // Postfix: Assign x to z, then increment x (z=6, x=7)
cout << "Prefix/Postfix: " << y << ", " << z << endl;
// 7. Ternary Operator
string status = (a > 10) ? "High" : "Low";
// 8. Bitwise Shifts
int shifted = val1 << 1; // Left shift: 5 * 2 = 10
// 9. Precedence Demo
PrecedenceDemo();
return 0;
}
/*
* Operator Precedence & Notes:
* 1. Parentheses () have highest precedence.
* 2. Logical AND (&&) higher than logical OR (||).
* 3. Assignment (=, +=) has very low precedence.
* 4. Always use parentheses when mixing bitwise and arithmetic operators.
* 5. Bitwise shifts are useful for performance-critical low-level manipulation.
* 6. Ternary operator can simplify basic conditional assignments.
* 7. Unary increment/decrement (x++, ++x) behavior depends on position.
* 8. Precedence rules should not be relied upon if they make the code unclear;
* use extra parentheses to clarify intent.
*/
</code></pre>
<div class="interactive-sim">
<h3>Operator Simulator</h3>
<button class="sim-button" onclick="
const out = document.getElementById('sim-output-cpp1_3');
out.innerHTML = 'x = 10; y = 3;<br>';
setTimeout(() => out.innerHTML += '>> x % y is ' + (10 % 3), 800);
">Run Simulation</button>
<div id="sim-output-cpp1_3" class="sim-output"></div>
</div>
</section>
<footer style="margin-top: 40px; padding: 20px; background: #f8f9fa; border-top: 1px solid #dee2e6;">
<div style="display: flex; justify-content: space-between;">
<a href="#" data-file="Cpp1_2.html" style="text-decoration: none; color: #6c757d;">← Previous: Variables & Primitives</a>
<a href="#" data-file="Cpp1_4.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: Control Flow: Conditionals →</a>
</div>
</footer>
</article>