简体   繁体   中英

compare fast two memory regions

How to compare fast two (variable size) memory regions as binary?

int compare (void *a,void *b,size_t size){
...
}


// reg1 > reg2
reg1 = 00001000000000000000000000000000000000000000 (binary);
reg2 = 00000100000000000000000000000000000000000000 (binary); 

if (compare(reg1,reg2,32) > 0){
  return true;
}else{
  return false;
};

All comparisons are performed using native binary because that is the internal representation, unless you need to compare the textual representation of a binary number.

For a platform specific answer, check the assembly language for your platform's processor. Many modern processors have assembly language instructions for comparing regions of memory.

As other commenters have stated, memcmp is your best choice because it should be optimized for your platform.

If you must write your own, I suggest loading a word from memory and comparing memory word by word until there is not a match. If you need more details, compare the words byte by byte to find where the exact difference is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM