Skip to main content

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
2 votes
1 answer
21 views

Casting different from bit manipulation

Casting between uints and bytes16 yields different results as that of manipulating the bits directly. Why? I thought that with casting we were just changing the representation from bits to bytes, and ...
dNyrM's user avatar
  • 928
0 votes
0 answers
28 views

Get value between 160-208 bits in uint256 by web3

I have uint256 representation of hex string 0x0064ababe1000000000700001ca050ee97607a20445e11b012877b0a4e113388 uint256 some1 = uint48(someAddress >> 208); uint256 some2 = uint48(someAddress &...
Jack Sparrow's user avatar
0 votes
1 answer
113 views

Updating values that are packed inside a uint256

I recently learned about packing smaller uints inside bigger uints. for example: uint64, uint64, uint8, uint120 all packed into a single uint256. my question is, how can i update one of the values ...
Nolan Jannotta's user avatar
0 votes
2 answers
132 views

How to implement negation in Solidity?

I know that the EVM uses the two's complement system to represent signed values, and that EVM words have 256 bits. How can I negate a number in Solidity, i.e. obtain its negation under the two's ...
Paul Razvan Berg's user avatar
0 votes
1 answer
17 views

Why does negation flip all high bits of a number expect for the least significant bit?

In his blog post on 512 bit division, Remco Bloemen makes the following assertion: ... negation flips all high bits of a number up to but not including the least significant one Why is that?
Paul Razvan Berg's user avatar
0 votes
1 answer
461 views

Why does padding add zeros to left, but left align adds zeros to right when performing bit manipulation?

I am not sure if this is a general bit manipulation related question or something specific to Ethereum. But I am reading a part of the ethereum specification here and I am confused by the bit related ...
Finlay Weber's user avatar
0 votes
1 answer
121 views

Finding max of a memory array in assembly

Practicing my solidity assembly skills and I'm using foundry to test this function. For most inputs it works, but fails when There is a 0 present in the array Possibly duplicate values present in the ...
zzzzz's user avatar
  • 1
2 votes
1 answer
400 views

How to get a specific hex char from a uint256?

lets say I have the following uint256 value: 0x70997970c51812dc3a010c7d01b50e0d17dc79c8000403 This uint256 is packed with various data that I would like to be able to parse. How would I extract the ...
dd0sxx's user avatar
  • 185
1 vote
1 answer
645 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 ...
MShakeG's user avatar
  • 1,733
1 vote
1 answer
2k 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 ...
ClementWalter's user avatar
0 votes
1 answer
666 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(...
MShakeG's user avatar
  • 1,733
3 votes
2 answers
892 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 ...
MShakeG's user avatar
  • 1,733
1 vote
1 answer
1k 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 ...
YulePale's user avatar
  • 1,853
1 vote
0 answers
34 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 ...
Paul Razvan Berg's user avatar
1 vote
1 answer
318 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) ...
Paul Razvan Berg's user avatar
1 vote
1 answer
372 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 ...
Paul Razvan Berg's user avatar
6 votes
1 answer
8k 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 >>= ...
Paul Razvan Berg's user avatar
5 votes
0 answers
1k 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. * * ...
smatthewenglish's user avatar
7 votes
3 answers
613 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?
eth's user avatar
  • 86.1k