# Questions tagged [bit-manipulation]

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

11 questions
Filter by
Sorted by
Tagged with
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 ...
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 ...
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(...
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 ...
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 ...
1 vote
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 ...
1 vote
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) ...
1 vote
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 ...
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 >>= ...