English 中文(简体)
Modulo multiplication (in C)
原标题:Modulo multiplication (in C)

具体地说,我有两个未签字的分类账(a,b),我想计算(a*b)%UINT_/2008/5(UINT_import被定义为最大未签署。 这样做的最佳方式是什么?

背景: 我需要书写一个能够仿效几何顺序的子体模块,从中读到后,将给我下一个要素(Modulo UINT_ Fair),而我发现的唯一解决办法是把目前的要素添加到自己的时代,同时增加使用以下逻辑(我用于计算顺序)。

for(int i=0; i<b; ++i){
  if(UINT_MAX - current_value > difference) {
    current_value += difference;
  } else {
    current_value = difference - (UINT_MAX - current_value);
  }

when current_value = a in the first iteration (and is updated in every iteration, and difference = a (always). Obviously this is not a intelligent solution. How would an intelligent person achieve this?

Thanks!

最佳回答

正如已经提到的那样,如果你有两倍的宽度,那么就有理由使用。

(unsigned int)(((unsigned long long)a * b) % UINT_MAX)

<代码>int为32 bits and long long 64(或更多)。 如果你没有较大的类型,你可以将因素分成比特维的半数、乘数和减少零件,并最终将其集合起来。 引证为32位未签名的:

a_low = a & 0xFFFF;  // low 16 bits of a
a_high = a >> 16;    // high 16 bits of a, shifted in low half
b_low = b & 0xFFFF;
b_high = b >> 16;
/*
 * Now a = (a_high * 65536 + a_low), b = (b_high * 65536 + b_low)
 * Thus a*b = (a_high * b_high) * 65536 * 65536
 *          + (a_high * b_low + a_low * b_high) * 65536
 *          + a_low * b_low
 *
 * All products a_i * b_j are at most (65536 - 1) * (65536 - 1) = UINT_MAX - 2 * 65536 + 2
 * The high product reduces to
 * (a_high * b_high) * (UINT_MAX + 1) = (a_high * b_high)
 * The middle products are a bit trickier, but splitting again solves:
 * m1 = a_high * b_low;
 * m1_low = m1 & 0xFFFF;
 * m1_high = m1 >> 16;
 * Then m1 * 65536 = m1_high * (UINT_MAX + 1) + m1_low * 65536 = m1_high + m1_low * 65536
 * Similar for a_low * b_high
 * Finally, add the parts and take care of overflow
 */
m1 = a_high * b_low;
m2 = a_low * b_high;
m1_low = m1 & 0xFFFF;
m1_high = m1 >> 16;
m2_low = m2 & 0xFFFF;
m2_high = m2 >> 16;
result = a_high * b_high;
temp = result + ((m1_low << 16) | m1_high);
if (temp < result)    // overflow
{
    result = temp+1;
}
else
{
    result = temp;
}
if (result == UINT_MAX)
{
    result = 0;
}
// I m too lazy to type out the rest, you get the gist, I suppose.

当然,如果你需要的是实际削减模块编号UINT_/2008/5/code>,因为“Toad”假定,“<代码>未在t上签名”的复制件就是如此。

问题回答

EDIT: As pointed out in the comments... this answer applies to Modulo MAX_INT+1 I ll leave it standing here, for future reference.

It s much simpeler than that:

Just multiply the two unsigned ints, the result will also be an unsigned int. Everything which didn t fit in the unsigned int is basically not there. So no need to do a modulo operation:

http://codepad.org/rYhalAoC”rel=“nofollow”> See example here

 #include <stdio.h>

 void main()
 {
     unsigned int a,b;
     a = 0x90000000;
     b = 2;

     unsigned int c = a*b;

     printf("Answer is %X
", c);
 }

回答:0x20000000 (因此本应为0x120000000,但答案已经缩小,就像你在模块操作中想要做的那样)





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签