utilities(C/C++)

4770 ワード

swapのもう一つの実装
int max, min;
n > m ? (max = n, min = m):(max = m, min = n);
                    //               ,    ,       

10進数の小数形(単精度、二精度)とIEEE 754でのバイナリ形の相互変換
IEEE 754——コンピュータにおける浮動小数点数の表示方法
std::bitset<32> float2bits(float n)
{
    _ULonglong nMem = *(unsigned __int64*)&n;
    return std::bitset<32>(nMem);
}
std::bitset<64> double2bits(double n)
{
    _ULonglong nMem = *(unsigned __int64*)&n;
    return std::bitset<64>(nMem);
}

float bits2float(std::bitset<32>& bs)
{
    return *(float*)&bs;
}
double bits2double(std::bitset<64>& bs)
{
    return *(double*)&bs;
}

hashString
unsigned long  cryptTable[0x500];

void prepareCryptTable()
{
    unsigned long idx1, idx2, k;
    unsigned long step = 0x100, n = 5;
    unsigned long seed = 0x00100001;
    for (idx1 = 0; idx1 < step; ++idx1)
    {
        for (idx2 = idx1, k = 0; k < 5; ++k, idx2 += step)
        {
            unsigned long tmp1, tmp2;
            seed = (seed * 125 + 3) % 0x2aaaab;
            tmp1 = (seed & 0xffff) << 0x10;
            seed = (seed * 125 + 3) % 0x2aaaab;
            tmp2 = (seed & 0xffff);
            cryptTable[idx2] = (tmp1 | tmp2);
        }
    }
}

unsigned long hashString(char* lpszFileName, unsigned long dwHashType)
{
    unsigned char* key = (unsigned char*)lpszFileName;
    unsigned long seed1 = 0x7fed7fed, seed2 = 0xeeeeeeee;
    int ch;
    while (*key)
    {
        ch = toupper(*key++);
        seed1 = cryptTable[(dwHashType << 8) + ch] ^ (seed1 + seed2);
        seed2 = ch + seed1 + seed2 + (seed2 << 5) + 3;
    }
    return seed1;
}

説明は十一を参照し、最初から最後までHash表アルゴリズムを解析する.