2
  function computePairId(bytes20 base, bytes20 quote) public view returns (bytes4) {

    bytes32 pairHash = sha256(abi.encodePacked(base ^ quote));

    bytes4 pairId;
    pairId[0] = pairHash[28];
    pairId[1] = pairHash[29];
    pairId[2] = pairHash[30];
    pairId[3] = pairHash[31];
    return pairId;
  }

However I get

SolcCompilationError: Zocr.sol:31:5: TypeError: Expression has to be an lvalue.
    pairId[0] = pairHash[28];
    ^-------^

in Solidity ^0.4.24;

1 Answer 1

1
  function computePairId(bytes20 base, bytes20 quote) public pure returns (bytes4) {

    bytes32 pairHash = sha256(abi.encodePacked(base ^ quote));

    bytes4 pairId;
    bytes4 x = bytes4(0xff000000);

    pairId ^= (x & pairHash[28]);
    pairId ^= (x & pairHash[29]) >> 8;
    pairId ^= (x & pairHash[30]) >> 16;
    pairId ^= (x & pairHash[31]) >> 24;

    return pairId;
  }
2
  • This code looks incorrect to me. (Why is the bytes4 initialized to something other than 0?)
    – user19510
    Commented Mar 22, 2019 at 20:56
  • By bytes4 I assume you mean x? The reason x is initialized to ff000000 is bc that sets the first 8 bits to 1. x & pairHash[i] then creates a bytes4 where the first 8 bits are the same as pairHash[i].
    – Akhil F
    Commented Mar 23, 2019 at 19:45

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.