乘以__int64的 [英] Multiplying __int64's

查看:170
本文介绍了乘以__int64的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释(详细)如何乘以两个__int64 obj并检查结果是否适合__int64。

Can someone explain to me (in detail) how to multiply two __int64 objs and check if the result will fit in __int64.

注意:不要使用任何编译器或处理器相关例程。

Note: Do not use any compiler or processor dependent routines.

推荐答案

不会假设 a $ c> b 是正的:

not assuming a and b are positive:

__int64 a,b;
//...
__int64  tmp_result = abs(a) * abs(b) ;
if (
    ( a && b ) &&
    (
     ( tmp_result < abs(a) || tmp_result < abs(b) ) ||
     ( tmp_result / abs(a) != abs(b)) ||
     ( a == TYPE_MIN && b != 1) ||
     ( b == TYPE_MIN && a != 1)
    )
   )
   std::cout << "overflow";
__int64 result = a * b;

EDIT:将代码添加到代码。

编辑:在我看来,只要(a& a * b / a!= b)

这篇关于乘以__int64的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆