Search Results
Search type | Search syntax |
---|---|
Tags | [tag] |
Exact | "words here" |
Author |
user:1234 user:me (yours) |
Score |
score:3 (3+) score:0 (none) |
Answers |
answers:3 (3+) answers:0 (none) isaccepted:yes hasaccepted:no inquestion:1234 |
Views | views:250 |
Code | code:"if (foo != bar)" |
Sections |
title:apples body:"apples oranges" |
URL | url:"*.example.com" |
Saves | in:saves |
Status |
closed:yes duplicate:no migrated:no wiki:no |
Types |
is:question is:answer |
Exclude |
-[tag] -apples |
For more details on advanced search visit our help page |
1
vote
2
answers
669
views
How to assign to negative numbers in yul?
Take this code snippet:
function powOfTen(uint256 x) internal pure returns (int256 result) {
assembly {
switch x
case 1 { result := -18 }
}
}
Which doesn't compile:
ParserErro …
1
vote
How to assign to negative numbers in yul?
An alternative to @alberto's solution is to use the sub instruction:
function powOfTen(uint256 x) internal pure returns (int256 result) {
assembly {
switch x
case 1 { result := sub …
6
votes
2
answers
5k
views
Does the unchecked { ... } block apply only to the current function?
Take the following code snippet:
pragma solidity ^0.8.0;
contract MyContract {
function foo() public returns (uint256 result) {
// do unsafe work
}
function bar() public returns …
1
vote
1
answer
382
views
Why doesn't the shift operation revert on overflow in Solidity ^0.8.0?
Take the following function:
solidity ^0.8.0;
function foo(uint256 x) external pure returns (uint256) {
uint256 y = x << 255;
return y;
}
If you set x = 2, the result is 2**256, which is abo …
1
vote
2
answers
1k
views
Is it possible to read constants from external contracts in Solidity?
Say you have a contract defined like this:
pragam solidity >=0.8.0;
contract Foo {
uint256 public constant MY_CONSTANT = 6174;
}
If I just import Foo in another contract, without inheriting from …
0
votes
1
answer
196
views
How to cast an array made up of user-defined value types to the underlying type in Solidity?
I have an array which is made up of user-defined value types:
type UD60x18 is uint256;
UD60x18 memory foo[] = new UD60x18[](2);
Now, I would like to turn this into uint256. Can I cast this array to u …
4
votes
1
answer
1k
views
Can division underflow or overflow in Solidity?
We know that:
Since Solidity v0.8, the compiler has checks for {over,under}flow by default for all arithmetic operations.
Division is an arithmetic operation that rounds down to the integer part of t …
4
votes
Accepted
Can division underflow or overflow in Solidity?
Yes, there is at least one situation where overflow can occur. I don't know about underflows though.
When you divide the minimum of a signed type by -1, you get the mirror image of that number in the …
2
votes
1
answer
507
views
Multiplying fixed-point types in Solidity
I would like to define a function like this:
function convert(ufixed x) external pure returns (uint256 result) {
result = x * 1e18;
}
Unfortunately, the code above does not compile:
TypeError: t …
1
vote
How to write an unchecked for loop?
There's now a nicer way to do this in Solidity v0.8.19, thanks to the addition of user-defined operators.
I wrote a value type UC that makes unchecked for loops more concise and more readable:
// SPDX …
1
vote
Accepted
Why doesn't the shift operation revert on overflow in Solidity ^0.8.0?
It's designed to be that way.
The shift operation is not classified as an arithmetic operation. As per the discussion I had with @chriseth in the Solidity gitter channel:
3
votes
2
answers
2k
views
How to write an unchecked for loop?
I would like to write a for loop like this:
for (uint256 i = 0; i < length; unchecked { i += 1 }) {
// ...
}
But unfortunately this does not compile (fails with an error "expected primary express …
8
votes
Accepted
How to write an unchecked for loop?
Note: as of Solidity v0.8.22, this optimization is no longer needed.
See this and this.
You can do this by putting the post-iteration increment operation at the end of the loop:
for (uint256 i = 0; i …
2
votes
Accepted
Is the modulo operation cheaper in unchecked arithmetic?
Yes, modulo is cheaper in unchecked arithmetic! Take the following code:
pragma solidity ^0.8.0;
contract Modulos {
function foo(uint256 x, uint256 y) external view returns (uint256 result, uint2 …
3
votes
Why does Openzeppelin's CountersUpgradeable library wrap arithmetic in an unchecked block?
It's cheaper to use unchecked blocks and check for overflows and underflows manually than let Solidity do it. Refer to Is the modulo operation cheaper in unchecked arithmetic? for more details.