-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperators.sol
More file actions
585 lines (503 loc) · 17.7 KB
/
Operators.sol
File metadata and controls
585 lines (503 loc) · 17.7 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
// ============================================================================
// SOLIDITY OPERATORS - Complete Reference
// From Zero to Professional
// ============================================================================
contract Operators {
// ============================================================================
// 1. ARITHMETIC OPERATORS
// ============================================================================
// Basic mathematical operations
// Solidity 0.8.0+ has built-in overflow/underflow protection
function arithmeticOperators(
uint256 a,
uint256 b
)
public
pure
returns (
uint256 addition,
uint256 subtraction,
uint256 multiplication,
uint256 division,
uint256 modulo,
uint256 exponentiation
)
{
addition = a + b; // Addition
subtraction = a - b; // Subtraction (will revert if a < b in uint)
multiplication = a * b; // Multiplication
division = a / b; // Division (will revert if b == 0)
modulo = a % b; // Modulo/Remainder (will revert if b == 0)
exponentiation = a ** b; // Exponentiation (a to the power of b)
return (
addition,
subtraction,
multiplication,
division,
modulo,
exponentiation
);
}
// Integer division (rounds down)
function integerDivision() public pure returns (uint256, uint256, uint256) {
return (
uint256(7) / 2, // 3 (not 3.5)
uint256(10) / 3, // 3
uint256(5) / 10 // 0
);
}
// Modulo examples
function moduloExamples() public pure returns (uint256, uint256, uint256) {
return (
10 % 3, // 1
17 % 5, // 2
20 % 4 // 0
);
}
// Exponentiation
function exponentiationExamples()
public
pure
returns (uint256, uint256, uint256)
{
return (
2 ** 3, // 8
10 ** 2, // 100
5 ** 0 // 1
);
}
// Negative numbers with signed integers
function signedArithmetic(
int256 a,
int256 b
)
public
pure
returns (
int256 addition,
int256 subtraction,
int256 multiplication,
int256 division
)
{
addition = a + b;
subtraction = a - b;
multiplication = a * b;
division = a / b;
return (addition, subtraction, multiplication, division);
}
// ============================================================================
// 2. COMPARISON OPERATORS
// ============================================================================
// Compare values and return boolean results
function comparisonOperators(
uint256 a,
uint256 b
)
public
pure
returns (
bool equal,
bool notEqual,
bool lessThan,
bool lessOrEqual,
bool greaterThan,
bool greaterOrEqual
)
{
equal = (a == b); // Equal to
notEqual = (a != b); // Not equal to
lessThan = (a < b); // Less than
lessOrEqual = (a <= b); // Less than or equal to
greaterThan = (a > b); // Greater than
greaterOrEqual = (a >= b); // Greater than or equal to
return (
equal,
notEqual,
lessThan,
lessOrEqual,
greaterThan,
greaterOrEqual
);
}
// Comparing addresses
function compareAddresses(
address addr1,
address addr2
) public pure returns (bool, bool) {
return (addr1 == addr2, addr1 != addr2);
}
// Comparing strings (must use keccak256)
function compareStrings(
string memory str1,
string memory str2
) public pure returns (bool) {
return keccak256(bytes(str1)) == keccak256(bytes(str2));
}
// ============================================================================
// 3. LOGICAL OPERATORS
// ============================================================================
// Boolean logic operations
// Use short-circuit evaluation for gas optimization
function logicalOperators(
bool a,
bool b
) public pure returns (bool andResult, bool orResult, bool notResult) {
andResult = a && b; // Logical AND (both must be true)
orResult = a || b; // Logical OR (at least one must be true)
notResult = !a; // Logical NOT (negation)
return (andResult, orResult, notResult);
}
// Short-circuit evaluation
function shortCircuitExample(uint256 x) public pure returns (bool) {
// If x == 0, second condition is NOT evaluated (saves gas)
return x == 0 || x > 10;
}
// Complex logical expressions
function complexLogic(
uint256 x,
uint256 y,
bool flag
) public pure returns (bool) {
return (x > 0 && y > 0) || (flag && x == y);
}
// ============================================================================
// 4. BITWISE OPERATORS
// ============================================================================
// Operations on individual bits
// Very gas-efficient for certain operations
function bitwiseOperators(
uint8 a,
uint8 b
)
public
pure
returns (
uint8 andResult,
uint8 orResult,
uint8 xorResult,
uint8 notResult,
uint8 leftShift,
uint8 rightShift
)
{
andResult = a & b; // Bitwise AND
orResult = a | b; // Bitwise OR
xorResult = a ^ b; // Bitwise XOR
notResult = ~a; // Bitwise NOT
leftShift = a << 2; // Left shift by 2 bits
rightShift = a >> 2; // Right shift by 2 bits
return (
andResult,
orResult,
xorResult,
notResult,
leftShift,
rightShift
);
}
// Practical bitwise examples
function bitwiseExamples() public pure returns (uint256, uint256, uint256) {
uint256 a = 12; // Binary: 1100
uint256 b = 10; // Binary: 1010
return (
a & b, // 1000 = 8 (AND)
a | b, // 1110 = 14 (OR)
a ^ b // 0110 = 6 (XOR)
);
}
// Bit shifting for multiplication/division by powers of 2
function bitShiftOptimization(
uint256 x
)
public
pure
returns (
uint256 multiply4,
uint256 multiply8,
uint256 divide4,
uint256 divide8
)
{
multiply4 = x << 2; // Multiply by 4 (2^2)
multiply8 = x << 3; // Multiply by 8 (2^3)
divide4 = x >> 2; // Divide by 4 (2^2)
divide8 = x >> 3; // Divide by 8 (2^3)
return (multiply4, multiply8, divide4, divide8);
}
// Check if specific bit is set
function isBitSet(
uint256 value,
uint256 position
) public pure returns (bool) {
return (value & (1 << position)) != 0;
}
// Set specific bit
function setBit(
uint256 value,
uint256 position
) public pure returns (uint256) {
return value | (1 << position);
}
// Clear specific bit
function clearBit(
uint256 value,
uint256 position
) public pure returns (uint256) {
return value & ~(1 << position);
}
// Toggle specific bit
function toggleBit(
uint256 value,
uint256 position
) public pure returns (uint256) {
return value ^ (1 << position);
}
// ============================================================================
// 5. ASSIGNMENT OPERATORS
// ============================================================================
// Assign and modify values
function assignmentOperators() public pure returns (uint256) {
uint256 x = 10; // Simple assignment
x += 5; // Addition assignment (x = x + 5)
x -= 3; // Subtraction assignment (x = x - 3)
x *= 2; // Multiplication assignment (x = x * 2)
x /= 4; // Division assignment (x = x / 4)
x %= 3; // Modulo assignment (x = x % 3)
return x;
}
// Bitwise assignment operators
function bitwiseAssignment() public pure returns (uint8) {
uint8 x = 12; // Binary: 1100
x |= 3; // OR assignment (x = x | 3)
x &= 14; // AND assignment (x = x & 14)
x ^= 5; // XOR assignment (x = x ^ 5)
x <<= 1; // Left shift assignment
x >>= 1; // Right shift assignment
return x;
}
// ============================================================================
// 6. INCREMENT/DECREMENT OPERATORS
// ============================================================================
// Increase or decrease by 1
// Prefix (++x) vs Postfix (x++) have different behaviors
function incrementDecrement()
public
pure
returns (
uint256 postIncrement,
uint256 preIncrement,
uint256 postDecrement,
uint256 preDecrement
)
{
uint256 x = 5;
// Post-increment: returns current value, then increments
postIncrement = x++; // postIncrement = 5, x becomes 6
// Pre-increment: increments first, then returns new value
x = 5; // Reset
preIncrement = ++x; // preIncrement = 6, x becomes 6
// Post-decrement: returns current value, then decrements
x = 5; // Reset
postDecrement = x--; // postDecrement = 5, x becomes 4
// Pre-decrement: decrements first, then returns new value
x = 5; // Reset
preDecrement = --x; // preDecrement = 4, x becomes 4
return (postIncrement, preIncrement, postDecrement, preDecrement);
}
// Increment in loops (prefix is slightly more gas-efficient)
function loopIncrement() public pure returns (uint256) {
uint256 sum = 0;
for (uint256 i = 0; i < 10; ++i) {
// Prefix increment
sum += i;
}
return sum;
}
// ============================================================================
// 7. TERNARY OPERATOR
// ============================================================================
// Conditional expression: condition ? valueIfTrue : valueIfFalse
// More gas-efficient than if-else for simple conditions
function ternaryOperator(uint256 x) public pure returns (string memory) {
return x > 10 ? "Greater than 10" : "Not greater than 10";
}
// Nested ternary
function nestedTernary(uint256 x) public pure returns (string memory) {
return
x > 100
? "High"
: x > 50
? "Medium"
: "Low";
}
// Ternary with calculations
function ternaryCalculation(uint256 x) public pure returns (uint256) {
return x > 0 ? x * 2 : 0;
}
// ============================================================================
// 8. DELETE OPERATOR
// ============================================================================
// Resets variable to its default value
// For integers: 0, for bool: false, for arrays: length 0
uint256 public storedValue = 100;
bool public storedBool = true;
function deleteOperator() public {
delete storedValue; // storedValue becomes 0
delete storedBool; // storedBool becomes false
}
// Delete array element (sets to default, doesn't remove)
function deleteArrayElement() public pure returns (uint256[] memory) {
uint256[] memory arr = new uint256[](5);
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
delete arr[1]; // arr[1] becomes 0, but array length stays 5
return arr; // [10, 0, 30, 0, 0]
}
// ============================================================================
// 9. TYPE CASTING OPERATORS
// ============================================================================
// Convert between types
function typeCasting()
public
pure
returns (
uint256 fromInt,
int256 fromUint,
uint8 fromUint256,
address fromUint160
)
{
// Explicit casting
int256 signedValue = -10;
fromInt = uint256(signedValue); // Careful: -10 becomes very large number
uint256 unsignedValue = 100;
fromUint = int256(unsignedValue); // Safe conversion
uint256 largeValue = 1000;
fromUint256 = uint8(largeValue); // Truncated to 232 (1000 % 256)
uint160 addressValue = 123456789;
fromUint160 = address(addressValue); // Convert to address
return (fromInt, fromUint, fromUint256, fromUint160);
}
// Address casting
function addressCasting(
address addr
) public pure returns (address payable payableAddr, uint160 addrAsUint) {
payableAddr = payable(addr); // Cast to payable address
addrAsUint = uint160(addr); // Cast to uint160
return (payableAddr, addrAsUint);
}
// Bytes casting
function bytesCasting() public pure returns (bytes32, bytes4, uint256) {
bytes32 b32 = "Hello";
bytes4 b4 = bytes4(b32); // Take first 4 bytes
uint256 asUint = uint256(b32); // Convert to uint256
return (b32, b4, asUint);
}
// ============================================================================
// 10. OPERATOR PRECEDENCE
// ============================================================================
// Order of operations (highest to lowest precedence)
// 1. Postfix increment/decrement: x++, x--
// 2. Prefix increment/decrement, unary: ++x, --x, +x, -x, !
// 3. Exponentiation: **
// 4. Multiplication, division, modulo: *, /, %
// 5. Addition, subtraction: +, -
// 6. Bitwise shift: <<, >>
// 7. Bitwise AND: &
// 8. Bitwise XOR: ^
// 9. Bitwise OR: |
// 10. Comparison: <, >, <=, >=
// 11. Equality: ==, !=
// 12. Logical AND: &&
// 13. Logical OR: ||
// 14. Ternary: ? :
// 15. Assignment: =, +=, -=, etc.
function precedenceExample()
public
pure
returns (uint256, uint256, uint256)
{
uint256 a = 2 + 3 * 4; // 14 (not 20) - multiplication first
uint256 b = (2 + 3) * 4; // 20 - parentheses override
uint256 c = 2 ** 3 * 2; // 16 (not 64) - exponentiation first
return (a, b, c);
}
function complexPrecedence() public pure returns (bool, bool) {
uint256 x = 10;
uint256 y = 20;
bool flag = true;
// && has higher precedence than ||
bool result1 = flag || (x > 5 && y < 30); // true (AND evaluated first)
// With parentheses for clarity
bool result2 = flag || (x > 5 && y < 30);
return (result1, result2);
}
// ============================================================================
// 11. PRACTICAL OPERATOR PATTERNS
// ============================================================================
// Swap values using XOR (no temporary variable)
function xorSwap(
uint256 a,
uint256 b
) public pure returns (uint256, uint256) {
a = a ^ b;
b = a ^ b;
a = a ^ b;
return (a, b);
}
// Check if number is power of 2
function isPowerOfTwo(uint256 x) public pure returns (bool) {
return x > 0 && (x & (x - 1)) == 0;
}
// Count set bits (number of 1s in binary)
function countSetBits(uint256 x) public pure returns (uint256) {
uint256 count = 0;
while (x > 0) {
count += x & 1;
x >>= 1;
}
return count;
}
// Find absolute value (for signed integers)
function absoluteValue(int256 x) public pure returns (int256) {
return x >= 0 ? x : -x;
}
// Safe percentage calculation
function percentage(
uint256 amount,
uint256 percent
) public pure returns (uint256) {
require(percent <= 100, "Invalid percentage");
return (amount * percent) / 100;
}
// Clamp value between min and max
function clamp(
uint256 value,
uint256 minVal,
uint256 maxVal
) public pure returns (uint256) {
return value < minVal ? minVal : (value > maxVal ? maxVal : value);
}
// ============================================================================
// KEY TAKEAWAYS FOR PROFESSIONAL DEVELOPERS:
// ============================================================================
// 1. Solidity 0.8.0+ has automatic overflow/underflow checks
// 2. Integer division rounds down (no floating point)
// 3. Bitwise operators are very gas-efficient
// 4. Use bit shifting for multiplication/division by powers of 2
// 5. Prefix increment (++i) is slightly more gas-efficient than postfix (i++)
// 6. Ternary operator is more gas-efficient than if-else
// 7. Short-circuit evaluation: && and || stop evaluating when result is determined
// 8. delete operator resets to default value, doesn't remove from arrays
// 9. Be careful with type casting - can cause unexpected results
// 10. Use parentheses to make operator precedence explicit
// 11. Bitwise operations useful for flags and gas optimization
// 12. XOR (^) useful for toggling and swapping
// 13. Understanding precedence prevents logical errors
// 14. Modulo (%) useful for cyclic operations and remainders
// ============================================================================
}