0
  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;

0
  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;
  }
  • This code looks incorrect to me. (Why is the bytes4 initialized to something other than 0?) – user19510 Mar 22 '19 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]. – Aakil Fernandes Mar 23 '19 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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