我们都知道 11 * 11 = 121,超长计算还是按照这种规则计算。
把前32位的计算了,再计算后面的32位,最后通过移位把正确的值放入对应的长整形中。
程序设计的思路就是简单的叠加
注意: unsigned long long u1 * unsigned long long u2 最后的结果是两个unsigned long long数据哦。
实例代码:
void ull_x_ull(unsigned long long dbP1, unsigned long long dbP2)
{
unsigned long long dbBuf1[2];
unsigned long long dbBuf2[2];
unsigned long long dbRes[2];
unsigned long long dbTmp[2];
unsigned long long dbTmpRes;
//dbP1 = 8589934592+1;
//dbP2 = 8589934592+1;
printf("sizeof(longlong) = %d\n", sizeof(dbBuf1));
printf("\ndbP1, dbP2 = %llx , %llx\n", dbP1, dbP2);
printf("dbP1, dbP2 = %llu , %llu\n", dbP1, dbP2);
dbBuf1[0] = (dbP1 & 0xFFFFFFFF00000000)>>32;
dbBuf1[1] = dbP1 & 0x00000000FFFFFFFF;
printf("dbBuf1 = %llx - %llx\n", dbBuf1[0], dbBuf1[1]);
dbBuf2[0] = (dbP2 & 0xFFFFFFFF00000000)>>32;
dbBuf2[1] = dbP2 & 0x00000000FFFFFFFF;
printf("dbBuf2 = %llx - %llx\n", dbBuf2[0], dbBuf2[1]);
//low *
dbRes[1] = dbBuf1[1] * dbBuf2[1];//result of low 32 bits
printf("dbRes[1] = %llx\n", dbRes[1]);
dbTmp[0] = dbBuf1[0] * dbBuf2[1];
dbTmp[1] = dbBuf1[1] * dbBuf2[0];
printf("dbTmp[0], dbTmp[1] = %llx , %llx\n", dbTmp[0], dbTmp[1]);
dbTmpRes = dbTmp[0] + dbTmp[1];//result of low 32 bits * high 32 bits
printf("dbTmpRes = %llx\n", dbTmpRes);
dbRes[1] = dbRes[1] + ((dbTmpRes & 0x00000000FFFFFFFF)<<32);
//high calc
dbRes[0] = dbBuf1[0] * dbBuf2[0];//result of high 32 bits
printf("dbRes[0] = %llx\n", dbRes[0]);
dbRes[0] = dbRes[0] + ((dbTmpRes & 0xFFFFFFFF00000000)>>32);//result of all + high 32 bits
printf("dbRes[0], dbRes[1] = %llx - %llx\n", dbRes[0], dbRes[1]);
printf("dbRes[0], dbRes[1] = %llu - %llu\n", dbRes[0], dbRes[1]);
printf("Result X: %llx%016llx\n", dbRes[0], dbRes[1]);
printf("Result L: %llu-%llu\n", dbRes[0], dbRes[1]);
}
int main(int argc)
{
ull_x_ull(8589934592+1, 8589934592+1);
}
文 | 力卉编程
网友评论