Questions tagged [bit-manipulation]

The manipulation of individual bits. Operators used may include bitwise AND, OR, XOR, NOT, left-shift, and right-shift.

Filter by
Sorted by
Tagged with
1
vote
0answers
10 views

What is the fastest way to implement the find first set algorithm in Solidity?

As per Wikipedia, a find first algorithm is: a bit operation that, given an unsigned machine word, designates the index or position of the least significant bit set to one in the word counting from ...
1
vote
1answer
11 views

What happens when the result of the shift operation does not fit in the type of the left operand?

For instance, take the following code: pragma solidity ^0.8.0; function foo() external pure returns (uint256, uint256, uint256) { uint256 i1 = uint256(2) << 255; uint256 i2 = uint256(4) ...
1
vote
1answer
17 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 ...
1
vote
1answer
41 views

What does the “>>=” operator do in Solidity?

I've seen this operator used in one of Mikhail Vladimirov's articles: function mostSignificantBit(uint x) public pure returns (uint r) { require (x > 0); if (x >= 2**128) { x >>= ...
3
votes
0answers
56 views

bitwise multiplication to reduce gas cost

This is the multiplication method from the SafeMath library. /** * @dev Returns the multiplication of two unsigned integers, reverting on * overflow. * * Counterpart to Solidity's `*...
7
votes
3answers
444 views

Why are there no bit shifting opcodes in Ethereum?

Why are there no bit shifting opcodes in Ethereum? What's the fastest and cheapest way (in terms of gas) to implement them?