mardi 17 mars 2015

How to tell if two 8-bit chars are gray codes in c++?


The problem is to tell if two 8-bit chars are gray codes(differ only in 1 bit) in C++? I found an elegant C++ solution:



bool isGray(char a, char b) {
int m = a ^ b;
return m != 0 && (m & (m - 1) & 0xff) == 0;
}


I was confused that what does the "& 0xff" do?




Aucun commentaire:

Enregistrer un commentaire