我想在右側應用一個無符號的移位,但我找不到這樣做的方式。有誰知道?BigInteger上的Java無符號bitshift
示例代碼:
BigInteger bi;
bi.shiftRight(7) // equals >> 7
如何申請>>> 7
?
重複?請stahp,我沒有我的答案。
我想在右側應用一個無符號的移位,但我找不到這樣做的方式。有誰知道?BigInteger上的Java無符號bitshift
示例代碼:
BigInteger bi;
bi.shiftRight(7) // equals >> 7
如何申請>>> 7
?
重複?請stahp,我沒有我的答案。
我會建議也許使用一種unsigned int int,以便允許使用運算符,因爲您希望使用並確保數據只有無符號。
我希望這可以澄清方法創建類似於未簽名的數據類型(因爲Java沒有)這裏是一個鏈接。https://stackoverflow.com/questions/4449100/unsigned-int-in-java/4449161#4449161 –
如果是負值,您總是可以否定。
private void test(String[] args) {
test(BigInteger.ONE.shiftLeft(10));
test(BigInteger.valueOf(-50));
}
private void test(BigInteger bigInteger) {
test1(bigInteger);
test1(bigInteger.negate());
}
private void test1(BigInteger bi) {
System.out.println(bi.toString(2)+" >> 5 = "+bi.shiftRight(5).toString(2));
}
注:請記住,BigInteger
是不變所以如果你對他們的數學必須保持返回的結果因爲數學沒有修改這個值,則返回計算的結果。
https://stackoverflow.com/questions/2727005/bitshifting-with-bigintegers-in-java –
這樣的操作符對BigInteger沒有意義;你可以擴展你的問題來添加你想使用這樣一個運算符,並給出幾種類型的輸入期望輸出的例子嗎? – DodgyCodeException
@DodgyCodeException:我也這麼認爲,但爲什麼不呢?正數可以像正常情況一樣移動,但負數應該變爲正數並被否定。 IOW,右移4,0x87654321(負數)將變爲0x08765432(正數)。 BigIntegers也可以模擬這一點。 –