protected bool FirstBitsMatch(byte a, byte b, int count) {
byte mask = 0;
if( count >= 8 ) { mask = 0xFF; }
else {
switch( count ) {
//This is the most common case, put it first
case 8:
mask = 0xFF;
break;
case 1:
mask = 0x80;
break;
case 2:
mask = 0xC0;
break;
case 3:
mask = 0xE0;
break;
case 4:
mask = 0xF0;
break;
case 5:
mask = 0xF8;
break;
case 6:
mask = 0xFC;
break;
case 7:
mask = 0xFE;
break;
default:
mask = 0xFF;
break;
}
}
//Now we have the mask:
int ai = a & mask;
int bi = b & mask;
return (ai == bi);
}
}