如何正确地将两个 long long int 相乘?

发布于 2024-09-15 07:41:55 字数 1591 浏览 8 评论 0原文

我想乘以 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");
}

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

正如你从我的评论中看到的,我想要两个 uint32 相乘的结果。 下半部分为 0x1,上半部分为 0xFFFFFFFE。我如何获得这些值?

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

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

运行代码时得到的输出:

4294967295
4294967295
1
4294967295

Edit2:

  printf("%i\n", sizeof(unsigned long));
  printf("%i\n", sizeof(unsigned long long)); 

返回

4
8

Edit 3: 感谢 Petesh 我找到了解决方案:

  printf("%lu\n", c & 0xFFFFFFFF);
  printf("%lu\n", (c >> 32));

I want to multiply long numbers which are given in a 2^32 basis. I already thought of an nice algorithm to do that, but unfortunatly I'm stuck. The situation I'm stuck at, is how I do multiply two long ints and represent it on the 2^32 basis.

#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");
}

Why is ULLONG_MAX the same as ULONG_MAX ? According to http://en.wikipedia.org/wiki/Limits.h#Member_constants it should be 18,446,744,073,709,551,615 I

As you can see from my comments, I want the result of the the multiplikation in the two uint32.
The lowerhalf would be 0x1 and the upper half 0xFFFFFFFE. How do I get these values?

(I found this question on SO, but it's not helpful in my situation because the answers given ar similiar to my ideas: Multiplying two long long ints C)

Edit:
My system is Windows XP 32 Bit. I'm using gcc 3.4.2 (mingw-special)

The output I do get while running the code:

4294967295
4294967295
1
4294967295

Edit2:

  printf("%i\n", sizeof(unsigned long));
  printf("%i\n", sizeof(unsigned long long)); 

returns

4
8

Edit 3:
Thanks to Petesh I was able to find the solution:

  printf("%lu\n", c & 0xFFFFFFFF);
  printf("%lu\n", (c >> 32));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

腹黑女流氓 2024-09-22 07:41:55

提示在系统中(“暂停”) - 你在 Windows 上?使用 Microsoft Visual C 运行时打印 long long 需要使用“%I64u”(即大写 i)。

这是基于SO问题 How do you printf an unsigned long long int(unsigned long long int 的格式说明符)?

The hint is in the system("pause") - you're on windows? Printing a long long using the Microsoft visual c runtime requires using '%I64u' (that's a capital i).

This is based on SO question How do you printf an unsigned long long int(the format specifier for unsigned long long int)?

兔姬 2024-09-22 07:41:55

不知道为什么你用你的(未指定的)编译器得到这些结果,但 Ubuntu 10 下的 gcc 给出:

4294967295
4294967295
18446744065119617025
18446744073709551615

最后两个是 0xfffffffe00000001 和 (264< /sup>-1) 分别如您所愿。

因此,也许可以考虑切换到更新的编译器。您可能使用的是 C99 之前的编译器。

只是出于兴趣,sizeof (unsigned long)sizeof (unsigned long long) 在您的系统上为您提供了什么。这将有助于解释你的问题。


由于您的 sizeof 似乎表明数据类型本身没问题(尽管这些可能无法解决问题 - 它们是通过 相当浅的网络搜索):

  • 尝试使用 "%I64u" 作为格式字符串,而不是 "%llu"。如果 MinGW 使用 MSVCRT 库,则可能需要真正的 64 位 printf 支持。
  • 确保您使用 -std=c99 进行编译。

Not sure why you're getting those results with your (unspecified) compiler but gcc under Ubuntu 10 gives:

4294967295
4294967295
18446744065119617025
18446744073709551615

with those last two being 0xfffffffe00000001 and (264-1) respectively, as you desire.

So maybe consider switching to a more up-to-date compiler. It may be you're using a pre-C99 compiler.

Just out of interest, what does sizeof (unsigned long) and sizeof (unsigned long long) give you on your system. This would go a long way towards explaining your problem.


A couple of other things to check since your sizeofs seem to indicate the data types themselves are okay (though these may not fix the problem - they were found with a fairly shallow web search):

  • Try using "%I64u" as the format string instead of "%llu". If MinGW is using the MSVCRT libs, that might be required for real 64-bit printf support.
  • Make sure you're compiling with -std=c99.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文