简体   繁体   English

如何将两个BigInteger变量比较为无符号?

[英]How to Compare two BigInteger variables as unsigned?

I have two BigInteger variables whose values are initialized from 8 byte arrays: 我有两个BigInteger变量,其值从8个字节的数组初始化:

maxOne: ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
midOne: 8000:8000:8000:8000:8000:8000:8000:8000

I would like to use a BigInteger comparison method that would return that maxOne is greater than midOne. 我想使用BigInteger比较方法,该方法将返回maxOne大于midOne。

However, I keep getting the opposite. 但是,我总是相反。 ie midOne is greater than maxOne. 即midOne大于maxOne。

Internally, I could see that they are stored as: 在内部,我可以看到它们存储为:

maxOne: maxOne:

mag[0]: 1
signum: -1

midOne midOne

mag[0]: 7FFF7FFF
mag[1]: 7FFF7FFF
mag[2]: 7FFF7FFF
mag[3]: 7FFF8000
signum: -1

I would think a "larger absolute" value is lesser than a "smaller absolute" value when both have signum=-1 (eg -3 is lesser than -1). 我认为当两个信号均具有signum = -1时(例如-3小于-1),“较大的绝对值”小于“较小的绝对值”。

But for some reason, maxOne.compareTo(midOne) returns -1. 但是由于某些原因, maxOne.compareTo(midOne)返回-1。

When examining their BigInteger.doubleValue() , I see that 当检查他们的BigInteger.doubleValue() ,我看到了

maxOne = -1.0
midOne = -1.7013858727242528 E38

Even if compareTo() were to use the doubleValue() to do the compare, maxOne > midOne. 即使compareTo()使用doubleValue()进行比较,maxOne> midOne。

So, why is the comparison returning the opposite? 那么,为什么比较却相反呢?

Either way, I am interested comparing them as two unsigned integers . 无论哪种方式,我都很有兴趣将它们比较为两个无符号整数 Is there a way to accomplish that? 有没有办法做到这一点?

I suggest to put a zero byte at the most significant place. 我建议将一个零字节放在最重要的位置。 So that no sign bit can creep in when creating the BigInteger. 这样,在创建BigInteger时就不会出现任何迹象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM