java 高位,Java长乘法的高位?

Is there any way to get the high half of the multiplication of two longs in Java? I.e. the part that vanishes due to overflow. (So the upper 64 bits of the 128-bit result)

I'm used to writing OpenCL code where the command mul_hi does exactly this: http://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/mul_hi.html

Since OpenCL can do it efficiently on my CPU, Java should be able to do so as well, but I can't find how I should do this (or even mimic its behaviour efficiently) in Java. Is this possible in Java, and if so, how?

解决方案

The accepted solution is wrong most of the time (66%), though the error is bounded (it can be smaller than the exact result by at most 2 and it can never be bigger). This comes from

ignoring the x_lo * y_lo product

first shifting and then adding x_hi * y_lo and x_lo * y_hi

My solution seems to always work for non-negative operands.

final long x_hi = x >>> 32;

final long y_hi = y >>> 32;

final long x_lo = x & 0xFFFFFFFFL;

final long y_lo = y & 0xFFFFFFFFL;

long result = x_lo * y_lo;

result >>>= 32;

result += x_hi * y_lo + x_lo * y_hi;

result >>>= 32;

result += x_hi * y_hi;

Tested on a billion random operands. There should be a special test for corner cases and some analysis.

Dealing with negative operands would be more complicated as it'd prohibit using the unsigned shift and force us to handle intermediate result overflow.

In case speed doesn't matter much (and it rarely does), I'd go for

BigInteger.valueOf(x).multiply(BigInteger.valueOf(y))

.shiftRight(64).longValue();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java 中可以使用 java.math.BigInteger 类来进行大整数乘法。 例如: ``` import java.math.BigInteger; public class Main { public static void main(String[] args) { BigInteger a = new BigInteger("1234567890"); BigInteger b = new BigInteger("9876543210"); BigInteger c = a.multiply(b); System.out.println(c); } } ``` 输出结果为: ``` 1219326311126352690 ``` ### 回答2: 大整数乘法是指在计算机中实现两个较大整数相乘的运算。由于整数的范围受限,较大的整数可能超出计算机所能表示的范围,因此无法直接使用乘法运算符进行计算。为了解决这个问题,需要使用其他数据结构和算法来实现大整数的乘法运算。 一种常见的解决方案是使用数组或字符串来表示大整数。通过将整数的每一位存储在数组的不同位置上,或通过将整数转化为字符串来进行存储。这样可以绕过计算机整数范围的限制,可以实现任意位数的整数运算。 在Java中,可以使用数组来表示大整数。实现大整数的乘法运算可以分为以下步骤: 1. 首先,将两个大整数转化为数组形式,并确定结果数组的度。 2. 然后,使用嵌套循环遍历两个大整数的数组,对每一位进行乘法运算,并将结果累加到结果数组的对应位置上。 3. 最后,对结果数组进行进位处理,即将大于10的数相应进位,并将结果转化为字符串形式输出。 需要注意的是,由于大整数的乘法可能导致结果的位数超过结果数组的度,因此可能需要进位处理。另外,还需要处理两个大整数的符号问题(正负)。 综上所述,大整数乘法是一种使用数组或字符串表示的数据结构,通过遍历两个大整数数组进行乘法运算,并进行进位处理和符号处理,最终得到结果的运算过程。在Java中可以通过实现相关的算法来实现大整数乘法运算。 ### 回答3: 大整数乘法,即计算两个大整数的乘积。在Java中,由于整数的位数是有限的,超出限制就会溢出,所以需要使用其他方式来进行大整数乘法。 常见的实现方式是使用字符串来表示大整数,并手动模拟乘法运算的过程。具体步骤如下: 1. 定义两个字符串来表示两个大整数,假设分别为num1和num2。我们需要逆序遍历两个字符串中的每个字符。 2. 创建一个整型数组result来保存乘法的结果,初始时都为0。 3. 循环遍历num1和num2的每一位数字,并进行乘法运算。乘法过程中要注意进位的处理。 a) 从num2的末尾开始,将num2的每一位与num1中的每一位相乘,将结果保存到result数组中正确的位置。 b) 在遍历过程中,通过i和j的和来确定result数组中的位置,以及保存进位。 4. 进行进位处理。遍历result数组,将每一位与10进行取模运算,结果保存在对应位置,并将进位加到下一位上。 5. 移除高位的0。当最高位为0时,我们要将其移除,使结果正确。 6. 将result数组转换成字符串,并返回结果即可。 这种方法不受整型数据位数限制,可以用于计算任意大整数的乘法。但是需要注意大整数的输入格式和异常情况处理,例如输入为空、0和负数的情况。 这就是用Java实现大整数乘法的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值