0

I'm trying to apply an unsigned shift on the right but I cannot find the way to do it. Is there anyone who knows?

Sample code:

BigInteger bi;

bi.shiftRight(7) // equals >> 7

How to apply >>> 7 ?

Duplicate? Please stahp, I don't have my answer.

Romeortec
  • 211
  • 1
  • 2
  • 11
  • 1
    https://stackoverflow.com/questions/2727005/bitshifting-with-bigintegers-in-java – Veselin Davidov Oct 19 '17 at 13:24
  • Such an operator doesn't make sense for BigInteger; can you expand your question to add what you want to use such an operator for, and give some examples of expect output for several types of input? – DodgyCodeException Oct 19 '17 at 13:43
  • @DodgyCodeException: I thought so too, but why not? positive numbers can be shifted like normal, but negative ones should become positive and be negated. IOW, shifting right by 4, 0x87654321 (negative) would become 0x08765432 (positive). That can be emulated for BigIntegers too. – Rudy Velthuis Oct 19 '17 at 14:31
  • @RudyVelthuis "That can be emulated for BigIntegers too." - How? What you have shown as an example is bit-shifting a *positive* number. -0x876543221 is negative, but 0x87654321 is positive. It's only considered negative when stored as a 32-bit two's complement number. But it's not, for example, negative if it were a 64-bit long. And BigIntegers are not 32-bit, they are not 64-bit, they are "infinite-bits" long. So for a BigInteger, 0xWhatever is always positive. – DodgyCodeException Oct 19 '17 at 14:44
  • I wonder if those people who flagged this question as a duplicate have actually *read* this question properly? The other question doesn't answer this one. This question may possibly help: https://stackoverflow.com/questions/34753668/shifting-negative-biginteger-value-java – DodgyCodeException Oct 19 '17 at 14:52
  • @DodgyCodeException: if you have a 32 bit integer, 0x87654321 is negative. Shift that, and the sign bit disappears, giving the positive value 0x08765432. Such behaviour can be emulated in a function. – Rudy Velthuis Oct 19 '17 at 15:03
  • @RudyVelthuis yes, 0x87654321 is a negative 32-bit int, but is *not* negative in a BigInteger representation. And this question is not asking about negative 32-bit integers; it's asking about negative BigIntegers. A negative BigInteger can only be represented using a minus sign. It doesn't make sense to think of BigInteger in terms of 2's complement. – DodgyCodeException Oct 19 '17 at 15:16
  • @Dodgy: no, but that doesn't matter. Java's BigInteger also **emulates** two's complement semantics for other operations, like or, xor etc. That means that it simply regards the signum bit as the sign bit and that is how `or` and `and` work, or the other shift. In other words, this can be emulated too. If I have a little more time, I'll show this in an answer. – Rudy Velthuis Oct 19 '17 at 15:42
  • @RudyVelthuis unfortunately you can't write an answer because some people incorrectly marked this question as a duplicate :-( but I get what you mean with bitwise operators such as and(). Even so, I challenge you to tell me what the value of `BigInteger.valueOf(-1).shiftRightUnsigned(2)` would be if such a method existed. – DodgyCodeException Oct 19 '17 at 16:04
  • @Dodgy: I see what you mean. – Rudy Velthuis Oct 20 '17 at 06:30

3 Answers3

2

As the BigInteger javadocs says:

The unsigned right shift operator (>>>) is omitted, as this operation makes little sense in combination with the "infinite word size" abstraction provided by this class.

Shadov
  • 5,421
  • 2
  • 19
  • 38
1

You can always negate if it's negative.

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));
}

NB: Remember that BigInteger is immutable so if you do maths on them you must keep the returned result because the maths does not modify the value, it returns the calculated result.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

I would advise perhaps using a type of unsigned int int in order to allow for the use of operators as you wish to use and insure data is only unsigned.

  • I hope this clarifies ways as to create data types similar to that of unsigned(as java has none) here is a link. https://stackoverflow.com/questions/4449100/unsigned-int-in-java/4449161#4449161 – Andrew Linington Oct 19 '17 at 13:33