如何正确地乘以两个长多头?

cit*_*nas 0 c multiplication unsigned-long-long-int

我想乘以2 ^ 32基数给出的长数.我已经想到了一个很好的算法来做到这一点,但不幸的是我被卡住了.我坚持的情况是,我如何乘以两个长的整数并在2 ^ 32的基础上表示它.

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef unsigned int uint32;
typedef unsigned long long uint64;
int main(int argc, char* argv[] )
{

  uint64 a = (uint64)ULONG_MAX;
  printf("%llu\n", a);
  uint64 b = (uint64)ULONG_MAX;  
  printf("%llu\n", b);  
  uint64 c = (uint64)(a*b);

  printf("%llu\n", c);  // prints 1. that would be to lower 32 bits of the results. the upper half is 0xFFFFFFFE

  printf("%llu\n", ULLONG_MAX);
  system("pause");
}
Run Code Online (Sandbox Code Playgroud)

为什么ULLONG_MAX与ULONG_MAX相同?根据http://en.wikipedia.org/wiki/Limits.h#Member_constants,它应该是18,446,744,073,709,551,615我

从我的评论中可以看出,我想要两个uint32中的multiplikation的结果.lowerhalf为0x1,上半部分为0xFFFFFFFE.我如何获得这些值?

(我在SO上发现了这个问题,但是对我的情况没有帮助,因为给出的答案与我的想法类似:乘以两个长的长C)

编辑: 我的系统是Windows XP 32位.我正在使用gcc 3.4.2(mingw-special)

我在运行代码时得到的输出:

4294967295
4294967295
1
4294967295
Run Code Online (Sandbox Code Playgroud)

EDIT2:

  printf("%i\n", sizeof(unsigned long));
  printf("%i\n", sizeof(unsigned long long)); 
Run Code Online (Sandbox Code Playgroud)

回报

4
8
Run Code Online (Sandbox Code Playgroud)

编辑3: 感谢Petesh,我找到了解决方案:

  printf("%lu\n", c & 0xFFFFFFFF);
  printf("%lu\n", (c >> 32));
Run Code Online (Sandbox Code Playgroud)

Pet*_*esh 5

提示是在系统中("暂停") - 你在Windows上?使用Microsoft visual c运行时打印很长时间需要使用'%I64u'(这是大写i).

这基于SO问题如何printf unsigned long long int(unsigned long long int的格式说明符)?