字符串的快速比对

2020-09-21  本文已影响0人  懒生活
typedef unsigned long long bit;
bit tobit(char* pchar)
{
    bit ret = 0;
    while (*pchar != 0)
    {
        ret |= bit((*pchar - 'a' + 1));
        ret = ret << 5;
        pchar++;
    }
    return ret;
}

void tochar(bit val, char* pchar)
{
    bit mask = 0x1f;
    int j = 0;
    for (int i = 10; i >= 0; i--)
    {
        bit curmask = mask << 5 * i;
        char secval = (val & curmask) >> 5 * i;
        if (secval == 0)
        {
            continue;
        }
        else
        {
            pchar[j] = secval - 1 + 'a';
            j++;
        }
    }
    pchar[j] = 0;
}

char getchar(bit val, int idx)
{
    int len = getlen(val);
    //idx 0 ==> len_th
    int realidx = len - idx;
    bit mask = 0x1f;
    bit curmask = mask << (realidx-1) * 5;
    char secval = (val & curmask) >> (realidx-1) * 5;
    return (secval - 1 + 'a');
}
bool isOneCharDiff(bit val1, bit val2)
{
    bit temp = val1 ^ val2;
    bit mask = 0x1f;
    int difcnt = 0;
    for (int i = 0; i < 10; i++)
    {
        if ((temp & mask) != 0)
        {
            difcnt++;
        }
        mask = mask << 5;
    }
    if (difcnt == 1)
        return true;
    else
        return false;
}

上一篇下一篇

猜你喜欢

热点阅读