160bit数值运算有关问题

160bit数值运算问题
对任意字符串使用SHA1哈希函数后可以得到一个160bit的数值,我把它存在 typedef uint8_t key[20] 这样一个数据结构里。现在我想要对这个值做这样一个运算:(key + 2^i) mod 2^160,就是key加上2的i次方再算它除以2的160次方的余数。我不知道这个应该怎样用程序实现,我现在仅仅知道key*2^i是可以通过key向左位移i个单位即key<<i来实现。请各位高手帮忙,谢谢!

------解决方案--------------------
可以单独分开运算嘛。(k+2^i)%(2^160) = ((k%(2^160)+(2^i)%(2^160))%(2^160))
那么现在就是使用k和2^i次方进行移位来得到结果了。
------解决方案--------------------
我瞎想的,不一定对

x % 2^k,相当于取x二进制表示的最后k位:
如,3%2^1 = [(0011) & 0001]= [0001] = 1

所以你的问题变成:

if (i >= 160)
(key + 2^i) % 2^160 = key % 2^160 = [key & (2^160-1)] = 直接取key的后160位
else
(key + 2^i) % 2^160 = [key & (2^160-1) | 2^i] = 直接取key的后160位+2^i
------解决方案--------------------
C/C++ code
#include <iostream>
#include <string>
using namespace std;
inline int compare(string str1, string str2) {
    if(str1.size() > str2.size()) //长度长的整数大于长度小的整数
        return 1;
    else if(str1.size() < str2.size())
        return -1;
    else
        return str1.compare(str2); //若长度相等,从头到尾按位比较,compare函数:相等返回0,大于返回1,小于返回-1
}
string ADD_INT(string str1, string str2) {//高精度加法
    string SUB_INT(string str1, string str2);
    int sign = 1; //sign 为符号位
    string str;
    if(str1[0] == '-') {
        if(str2[0] == '-') {
            sign = -1;
            str = ADD_INT(str1.erase(0, 1), str2.erase(0, 1));
        } else {
            str = SUB_INT(str2, str1.erase(0, 1));
        }
    } else {
        if(str2[0] == '-')
            str = SUB_INT(str1, str2.erase(0, 1));
        else {
            //把两个整数对齐,短整数前面加0补齐
            string::size_type l1, l2;
            int i;
            l1 = str1.size(); l2 = str2.size();
            if(l1 < l2) {
                for(i = 1; i <= l2 - l1; i++)
                    str1 = "0" + str1;
            } else {
                for(i = 1; i <= l1 - l2; i++)
                    str2 = "0" + str2;
            }
            int int1 = 0, int2 = 0; //int2 记录进位
            for(i = str1.size() - 1; i >= 0; i--) {
                int1 = (int(str1[i]) - '0' + int(str2[i]) - '0' + int2) % 10;
                int2 = (int(str1[i]) - '0' + int(str2[i]) - '0' +int2) / 10;
                str = char(int1 + '0') + str;
            }
            if(int2 != 0) str = char(int2 + '0') + str;
        }
    }
    //运算后处理符号位
    if((sign == -1) && (str[0] != '0'))
        str = "-" + str;
    return str;
}
string SUB_INT(string str1, string str2) {//高精度减法
    string MUL_INT(string str1, string str2);
    int sign = 1; //sign 为符号位
    string str;
    int i;
    if(str2[0] == '-')
        str = ADD_INT(str1, str2.erase(0, 1));
    else {
        int res = compare(str1, str2);
        if(res == 0) return "0";
        if(res < 0) {
            sign = -1;
            string temp = str1;
            str1 = str2;
            str2 = temp;
        }
        string::size_type tempint;
        tempint = str1.size() - str2.size();
        for(i = str2.size() - 1; i >= 0; i--) {
            if(str1[i + tempint] < str2[i]) {
                str1[i + tempint - 1] = char(int(str1[i + tempint - 1]) - 1);
                str = char(str1[i + tempint] - str2[i] + ':') + str;
            } else
                str = char(str1[i + tempint] - str2[i] + '0') + str;
        }
        for(i = tempint - 1; i >= 0; i--)
            str = str1[i] + str;
    }
    //去除结果中多余的前导0
    str.erase(0, str.find_first_not_of('0'));
    if(str.empty()) str = "0";
    if((sign == -1) && (str[0] != '0'))
        str = "-" + str;
    return str;
}
string MUL_INT(string str1, string str2) {//高精度乘法
    int sign = 1; //sign 为符号位
    string str;
    if(str1[0] == '-') {
        sign *= -1;
        str1 = str1.erase(0, 1);
    }
    if(str2[0] == '-') {
        sign *= -1;
        str2 = str2.erase(0, 1);
    }
    int i, j;
    string::size_type l1, l2;
    l1 = str1.size(); l2 = str2.size();
    for(i = l2 - 1; i >= 0; i --) {  //实现手工乘法
        string tempstr;
        int int1 = 0, int2 = 0, int3 = int(str2[i]) - '0';
        if(int3 != 0) {
            for(j = 1; j <= (int)(l2 - 1 - i); j++)
                tempstr = "0" + tempstr;
            for(j = l1 - 1; j >= 0; j--) {
                int1 = (int3 * (int(str1[j]) - '0') + int2) % 10;
                int2 = (int3 * (int(str1[j]) - '0') + int2) / 10;
                tempstr = char(int1 + '0') + tempstr;
            }
            if(int2 != 0) tempstr = char(int2 + '0') + tempstr;
        }
        str = ADD_INT(str, tempstr);
    }
    //去除结果中的前导0
    str.erase(0, str.find_first_not_of('0'));
    if(str.empty()) str = "0";
    if((sign == -1) && (str[0] != '0'))
        str = "-" + str;
    return str;
}
string DIVIDE_INT(string str1, string str2, int flag) {//高精度除法
    //flag = 1时,返回商; flag = 0时,返回余数
    string quotient, residue; //定义商和余数
    int sign1 = 1, sign2 = 1;
    if(str2 == "0") {  //判断除数是否为0
        quotient = "ERROR!";
        residue = "ERROR!";
        if(flag == 1) return quotient;
        else return residue;
    }
    if(str1 == "0") { //判断被除数是否为0
        quotient = "0";
        residue = "0";
    }
    if(str1[0] == '-') {
        str1 = str1.erase(0, 1);
        sign1 *= -1;
        sign2 = -1;
    }
    if(str2[0] == '-') {
        str2 = str2.erase(0, 1);
        sign1 *= -1;
    }
    int res = compare(str1, str2);
    if(res < 0) {
        quotient = "0";
        residue = str1;
    } else if(res == 0) {
        quotient = "1";
        residue = "0";
    } else {
        string::size_type l1, l2;
        l1 = str1.size(); l2 = str2.size();
        string tempstr;
        tempstr.append(str1, 0, l2 - 1);
        //模拟手工除法
        for(int i = l2 - 1; i < l1; i++) {
            tempstr = tempstr + str1[i];
            tempstr.erase(0, tempstr.find_first_not_of('0'));//zhao4zhong1添加
            if(tempstr.empty()) tempstr = "0";//zhao4zhong1添加

            for(char ch = '9'; ch >= '0'; ch --) { //试商
                string str;
                str = str + ch;
                if(compare(MUL_INT(str2, str), tempstr) <= 0) {
                    quotient = quotient + ch;
                    tempstr = SUB_INT(tempstr, MUL_INT(str2, str));
                    break;
                }
            }
        }
        residue = tempstr;
    }
    //去除结果中的前导0
    quotient.erase(0, quotient.find_first_not_of('0'));
    if(quotient.empty()) quotient = "0";
    if((sign1 == -1) && (quotient[0] != '0'))
        quotient = "-" + quotient;
    if((sign2 == -1) && (residue[0] != '0'))
        residue = "-" + residue;
    if(flag == 1) return quotient;
    else return residue;
}
string DIV_INT(string str1, string str2) {//高精度除法,返回商
    return DIVIDE_INT(str1, str2, 1);
}
string MOD_INT(string str1, string str2) {//高精度除法,返回余数
    return DIVIDE_INT(str1, str2, 0);
}
int main() {
    char ch;
    string s1, s2, res;
    while(cin >> ch) {
        cin >> s1 >> s2;
        switch(ch) {
        case '+':  res = ADD_INT(s1, s2); break;
        case '-':  res = SUB_INT(s1, s2); break;
        case '*':  res = MUL_INT(s1, s2); break;
        case '/':  res = DIV_INT(s1, s2); break;
        case '%':  res = MOD_INT(s1, s2); break;
        default :  break;
        }
        cout << res << endl;
    }
    return(0);
}