33,028
社区成员
发帖
与我相关
我的任务
分享Long.toBinaryString(long i);void hash(unsigned char *str, unsigned long out[64] )
{
unsigned long hash = 5381;
int c;
while (c = *str++)
hash = ((hash < < 5) + hash) + c; /* hash * 33 + c */
hash;
for( int i = 63; i >=0; i-- )
out[63-i] = (hash>>i)&0x1;
} private static int _hashCode(this byte[] oldValue)
{
//一般编码都以字节为基本单位,也就是说基本单位长度为8bit;
//常用编码可能比较集中,造成编码中出现伪固定位(大多时候某些固定位都是同一值)
//采用移位的方式:当移位量为1或7时,一般只能覆盖掉1个固定位;当移位量为3或5时,一般能覆盖掉3个固定位;所以本程序使用的移位量为8x+5/3
//由于64=5+59=13+3*17=3*7+43=29+5*7=37+3*3*3=5*9+19=53+11=61+3,其中(5+59),(53+11),(61+3)为素数对成为最佳移位量,本程序选择中性素数对53+11
//由于32=5+3*3*3=13+19=3*7+11=29+3,其中(13+19),(29+3)为素数对成为最佳移位量,本程序选择中性素数对13+19
ulong code = 0;
int length = oldValue.Length, end = length & 7;
for (int i = (length >> 3) - 1; i >= 0; i--) code = ((code ^= BitConverter.ToUInt64(oldValue, i << 3)) << 53) + (code >> 11);
if (length > 7) code = ((code << 19) + ((code & 0xffff) >> 13)) ^ (code >> 32);
if (end >= 4)
{
code = ((code ^= (ulong)BitConverter.ToUInt32(oldValue, length)) << 19) + (code >> 13);
length += 4;
end -= 4;
}
if (end != 0)
{
ulong bytes = (ulong)oldValue[length];
switch (end)
{
case 1:
code = ((code ^= (bytes << 24) + (bytes << 16) + (bytes << 8) + bytes) << 19) + (code >> 13);
break;
case 2:
code = ((code ^= ((bytes += (ulong)oldValue[length + 1] << 8) << 16) + bytes) << 19) + (code >> 13);
break;
case 3:
ulong middle = ((ulong)oldValue[length + 1] << 8) + ((ulong)oldValue[length + 2] << 16);
code = ((code ^= (bytes + middle) ^ (middle << 7)) << 19) + (code >> 13);
break;
}
}
return (int)(code & 0x7fffffff);
}
}unsigned long hash(unsigned char *str)
{
unsigned long hash = 5381;
int c;
while (c = *str++)
hash = ((hash < < 5) + hash) + c; /* hash * 33 + c */
return hash;
} unsigned long
hash(unsigned char *str)
{
unsigned long hash = 5381;
int c;
while (c = *str++)
hash = ((hash < < 5) + hash) + c; /* hash * 33 + c */
return hash;
}