4

If I were trying to make a synthetic unsigned integer_512, but the largest supported integer in Ethereum is u256, how would I combine 2 u256 to make a single u512 in solidity?

If I were also looking to overload operators like gt, lt, eq, read/show/tostring, being mappable keys, on this joint data type---all of the functions that exist on a base data type, where is a good template for how to do this correctly or get started?

Thank you

1

1 Answer 1

3

If you're only interested in those three comparison operators, then it's pretty easy:

struct uint512 {
    uint256 hi;
    uint256 lo;
}

function eq(uint512 x, uint512 y) internal pure returns (bool) {
    return x.hi == y.hi && x.lo == y.lo;
}

function lt(uint512 x, uint512 y) internal pure returns (bool) {
    return x.hi < y.hi || (x.hi == y.hi && x.lo < y.lo);
}

function gt(uint512 x, uint512 y) internal pure returns (bool) {
    return x.hi > y.hi || (x.hi == y.hi && x.lo > y.lo);
}

Your Answer

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

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