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
0 votes
1 answer
73 views

How to convert default two's complement representation to sign magnitude form?

According to this answer the EVM by default uses two's complement notation for handling signed integers. However for my application I would like to work with the sign-magnitude representation where ...
user avatar
  • 170
0 votes
1 answer
349 views

How to bit shift a bytes in solidity / assembly?

I can bit shift uint_n_ easily but I don't manage to bit shift a bytes memory myBytes. When doing myBytes << i I have a compile error and when using assembly I receive 0. See my MWE for assembly ...
user avatar
0 votes
1 answer
148 views

How to convert uint8 to uint256 bitmap in solidity?

Say I have a uint8 with binary value 0000_1001 i.e. 8+1=9 in decimal. How do I convert this uint8 in a gas efficient way to a uint256 which represents a bitmap?, such that in this example the 9th bit(...
user avatar
  • 170
3 votes
2 answers
162 views

What is the most gas efficient way to get the value of a bit in an unsigned integer?

Say I have an uint16 _16bits with binary value 1110_1011_0001_0001, what would be the most gas efficient operation to get the value of any particular index in the range of [0, 15]? For clarity: using ...
user avatar
  • 170
0 votes
1 answer
291 views

How to deal with 256 bit numbers in javascript and perfom bitwise operations on them? [closed]

How to deal with 256 bit numbers in javascript and perform bitwise operations on them? From here I have read that JavaScript stores numbers as 64 bits floating point numbers, but all bitwise ...
user avatar
  • 1,243
1 vote
0 answers
16 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 ...
user avatar
1 vote
1 answer
90 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) ...
user avatar
1 vote
1 answer
177 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 ...
user avatar
4 votes
1 answer
3k 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 >>= ...
user avatar
4 votes
0 answers
527 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 `*` operator. * * ...
user avatar
7 votes
3 answers
535 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?
user avatar
  • 77.9k