I am testing a smart contract in Remix and I get the following error when trying to compare two bytes memory using "==":

TypeError: Operator == not compatible with types bytes memory and bytes memory.

This is my code :

function removeCertificate(bytes memory CertificateHash, address holder) external 
    isTheProviderOrHimself(holder, CertificateHash) 
    address provider = _CertificatesPerHolder[holder]._CertificateFromProvider[CertificateHash];
    bytes[] memory listOfCert = _CertificatesPerHolder[holder]._ListOfCertificates;
    for(uint i=0; i < listOfCert.length; i++){
        if(CertificateHash == listOfCert[i]){
            ArrayRemoveResize(i, _CertificatesPerHolder[holder]._ListOfCertificates);
    delete _CertificatesPerHolder[holder]._CertificateFromProvider[CertificateHash];
    emit _RemoveCertificateIdEvent(provider, holder);


I get the error at line : " if(CertificateHash == listOfCert[i]){"

1 Answer 1


listOfCert is of type bytes, listOfCert[i] is one element of those bytes. CertificateHash is of type bytes. So you cannot compare bytes to a single byte. Remix must have drawn your attention to that.

If you want to compare two bytes values, you can either check that they are of equal lengths, and if they are then proceed to loop through the elements one by one. But since two equal byte sequences will have equal keccak256 hashes, a more gas-efficient way to compare two byte sequences in Solidity is to compare their hashes:

bytes memory a = hex"00112233";
bytes memory b = hex"00112233";
bool areEqual = keccak256(a) == keccak256(b);

You could even try:

bool areEqual = a.length == b.length && keccak256(a) == keccak256(b);

This might bring some gains if you will often be comparing long bytes sequences of unequal lengths, at the cost of extra code.

Best would be to implement all three methods and compare how gas usage varies for different scenarios, and choose the best one to suit your needs.

  • Thank you very much for your reply. Actually I wanted listOfCert to be an array of "bytes" which is itself an array of "byte", I guess I was trying to implement a two dimensional array of "byte" where the first dimension is of dynamic length... May 20, 2021 at 13:48
  • Thats exactly my problem, it seems when we storing bytes in a array of bytes ,it stored them as byte, so how can we create an array which has elements of bytes, instead of byte??
    – Alireza
    Jan 8 at 8:48

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.