1

this operation uint256 pow2 = d & -d is from FullMath.sol

 function fullDiv(
        uint256 l,
        uint256 h,
        uint256 d
    ) private pure returns (uint256) {
        uint256 pow2 = d & -d;   
        d /= pow2;      
        .....
    }

I tested this operation with 20 and -20.

    00010100 = 20
    11101100 = -20

its bitwise and result is (If I am not mistaken so far)

 00010100 & 11101100=00000100

Why is this significant? Uniswap named variable AS pow2 but all the numbers from binary are already the power of 2. After the bitwise operation, d is divided by pow2=d & -d. I am trying to understand, what is the point of this division.

3

1 Answer 1

1

What is the importance of d /= pow2

The calculation uint256 pow2 = d & -d is the greatest power of 2 that divides d.

With d /= pow2 it is removing the zeros to the right. It is the "shifting" to the right both numerator and denominator. For example 51 / 20 = (51 / 4) / (20 / 4) = 12.75 / 5.

It is an optimization.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.