Big Integer - 这段代码在做什么?

Can*_*oss 1 java chord

我明白这个问题很奇怪,不是我惯用的风格.我目前正在使用peersim中的和弦实现做一个项目.下面的代码显示了一个大整数,并在其上执行了一些操作.我chordId是一个对象的哈希,为什么要使用add()?这个用途的目的是什么?

BigInteger base;
if (j == 0)
    base = BigInteger.ONE;
else {
    base = BigInteger.valueOf(2);
    for (int exp = 1; exp < j; exp++) {
        base = base.multiply(BigInteger.valueOf(2));
    }
}
BigInteger pot = cp.chordId.add(base);
Run Code Online (Sandbox Code Playgroud)

在他之前,和弦Id只是idlength的随机整数,它是128位.

因此,我的问题是上面add()用于??? 的代码段是什么?

[编辑]

为了使这个问题更加清晰,我们将其置于一个透视图中:

cp.fingerTable[j] = findId(pot, 0, Network.size() - 1);
Run Code Online (Sandbox Code Playgroud)

被调用,它试图找到Pot的Id但是它总是返回错误,因为在这个方法中创建的chordId不存在.我不确定如何更换锅或是否完全取出锅.

[EDIT2]

findId 看起来像这样(这不是我的代码,因此我的困惑:))

public Node findId(BigInteger id, int nodeOne, int nodeTwo) {
    if (nodeOne >= (nodeTwo - 1))
        return Network.get(nodeOne);
    int middle = (nodeOne + nodeTwo) / 2;
    if (((middle) >= Network.size() - 1))
        System.out.print("ERROR: Middle is bigger than Network.size");
    if (((middle) <= 0))
        return Network.get(0);
    try {
        BigInteger newId = ((ChordProtocol) ((Node) Network.get(middle))
                .getProtocol(pid)).chordId;
        BigInteger lowId;
        if (middle > 0)
            lowId = ((ChordProtocol) ((Node) Network.get(middle - 1))
                    .getProtocol(pid)).chordId;
        else
            lowId = newId;
        BigInteger highId = ((ChordProtocol) ((Node) Network
                .get(middle + 1)).getProtocol(pid)).chordId;
        if (id.compareTo(newId) == 0
                || ((id.compareTo(newId) == 1) && (id.compareTo(highId) == -1))) {
            return Network.get(middle);
        }
        if ((id.compareTo(newId) == -1) && (id.compareTo(lowId) == 1)) {
            if (middle > 0)
                return Network.get(middle - 1);
            else
                return Network.get(0);
        }
        if (id.compareTo(newId) == -1) {
            return findId(id, nodeOne, middle);
        } else if (id.compareTo(newId) == 1) {
            return findId(id, middle, nodeTwo);
        }
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

Nul*_*ion 5

这是代码看起来像是base一个int而不是BigInteger:

int base;
if (j == 0)
    base = 1;
else {
    base = 2;
    for (int exp = 1; exp < j; exp++) {
        base = base * 2;
    }
}
int pot = cp.chordId + base;
Run Code Online (Sandbox Code Playgroud)

实际上整个代码段可以替换为:

BigInteger base = BigInteger.valueOf(2).pow(j);
BigInteger pot = cp.chordId.add(base);
Run Code Online (Sandbox Code Playgroud)

这意味着它相当于:

int base = (int) Math.pow(2, j);
int pot = cp.chordId + base;
Run Code Online (Sandbox Code Playgroud)

它基本上增加了2 Ĵcp.chordId