Operators
Solidity's operators look like those of C or JavaScript, with a few differences that come from working with fixed-size integers and no floating point.
Arithmetic and assignment
| Operator | Example | Result for a = 17, b = 5 |
|---|---|---|
+ - * |
a + b |
22 |
/ |
a / b |
3 (integer division) |
% |
a % b |
2 |
** |
b ** 3 |
125 |
+= -= *= /= %= |
a += b |
a becomes 22 |
++ -- |
a++ |
increments by 1 |
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Main {
function main() external pure {
uint256 a = 17;
uint256 b = 5;
console.log("a + b =", a + b);
console.log("a - b =", a - b);
console.log("a * b =", a * b);
console.log("a / b =", a / b);
console.log("a % b =", a % b);
console.log("b ** 3 =", b ** 3);
uint256 counter = 10;
counter += 5;
counter *= 2;
counter--;
console.log("counter =", counter);
}
}a + b = 22
a - b = 12
a * b = 85
a / b = 3
a % b = 2
b ** 3 = 125
counter = 29
Doing math without decimals
Because there are no fractions, the order of operations changes the result. Multiply first, then divide; otherwise the division throws away precision:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Main {
function main() external pure {
uint256 price = 1999; // in cents
uint256 feePercent = 3;
uint256 wrong = price / 100 * feePercent; // 1999 / 100 = 19 first
uint256 right = price * feePercent / 100; // 5997 / 100
console.log("divide first :", wrong);
console.log("multiply first:", right);
// percentages with more precision: basis points (1 bp = 0.01%)
uint256 feeBps = 275; // 2.75%
console.log("2.75% of 1999 :", price * feeBps / 10_000);
}
}divide first : 57
multiply first: 59
2.75% of 1999 : 54
Tip: Money is usually represented with many decimals hidden in the integer: 1 ether is
10**18wei, and most tokens use 18 decimals too. Percentages are often expressed in basis points (1/100 of a percent).
Comparison and logic
| Operator | Meaning |
|---|---|
== != |
equal, not equal |
< <= > >= |
ordering (integers, addresses, fixed bytes, enums) |
&& || ! |
logical and, or, not (with short-circuit) |
cond ? x : y |
ternary: x if cond is true, otherwise y |
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Main {
function max(uint256 x, uint256 y) internal pure returns (uint256) {
return x >= y ? x : y;
}
function main() external pure {
uint256 age = 20;
bool hasTicket = true;
console.log("adult :", age >= 18);
console.log("can enter :", age >= 18 && hasTicket);
console.log("child or VIP :", age < 12 || !hasTicket);
console.log("max(7, 42) :", max(7, 42));
console.log("label :", age >= 65 ? "senior" : "regular");
}
}adult : true
can enter : true
child or VIP : false
max(7, 42) : 42
label : regular
Strings can't be compared with ==. To compare two strings, compare their hashes: keccak256(bytes(a)) == keccak256(bytes(b)), as shown in the Strings & Bytes lesson.
Bitwise operators
Bit operations pack several flags into one number, which is useful for permission sets:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Main {
uint8 constant CAN_READ = 1; // 0b001
uint8 constant CAN_WRITE = 2; // 0b010
uint8 constant CAN_DELETE = 4; // 0b100
function main() external pure {
uint8 perms = CAN_READ | CAN_WRITE; // set two flags
console.log("perms :", perms);
console.log("can write? ", perms & CAN_WRITE != 0);
console.log("can delete? ", perms & CAN_DELETE != 0);
perms = perms & ~CAN_WRITE; // clear a flag
console.log("after revoke :", perms);
console.log("toggle delete :", perms ^ CAN_DELETE);
console.log("1 << 8 (uint256):", uint256(1) << 8);
}
}perms : 3
can write? true
can delete? false
after revoke : 1
toggle delete : 5
1 << 8 (uint256): 256
Operator precedence
The rules are mostly as in C: ** binds tightest, then * / %, then + -, then shifts, then &, ^, |, then comparisons, then &&, ||, the ternary and assignments. Note one surprise: in Solidity, & binds tighter than ==, so perms & CAN_WRITE != 0 works as intended (unlike in C). When in doubt, add parentheses; they cost nothing.
The delete operator
delete x resets a variable to its zero value. It works on any type, including arrays, structs and mapping entries:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Main {
uint256 public score = 99;
address public winner = address(0xBEEF);
function main() external {
delete score;
delete winner;
console.log("score :", score);
console.log("winner:", winner);
}
}score : 0
winner: 0x0000000000000000000000000000000000000000
Deleting storage values also refunds a little gas, since it frees up storage.
Test yourself
With uint256 price = 1999, which expression gives a 3% fee of 59?
price / 100 * 3price * 3 / 100price * (3 / 100)price % 3
How do you check if two strings a and b are equal?
a == ba.equals(b)keccak256(bytes(a)) == keccak256(bytes(b))string.compare(a, b)
What does delete count; do to uint256 count = 7?
- Removes the variable from the contract
- Sets it to 0
- Sets it to
type(uint256).max - Makes it undefined