These methods work wrong if the number contains digits after decimal point.
The scale for result is always set to 0.
This behaviour is not compatible with specification.
==== Here is the minimized test demonstrating the bug ====
import java.lang.Bignum;
class java_lang_Bignum_shiftLeft {
public static void main(String args[]) {
Bignum b1=new Bignum("1.1");
Bignum b2=b1.shiftLeft(1);
Bignum b3=b1.shiftRight(1);
System.out.println(b2); //should print "2.2" but prints "22"
System.out.println(b3); //should print "0.5" but prints "5"
}
}
==== Here is the output of the test ====
22
5
==== Here is the part of java language specification ====
public Bignum shiftRight(int shiftBits)
Returns a new Bignum whose value is calculated by shifting this Bignum
to the left by the number of bits given in shiftBits
Parameters:
shiftBits - the number of bits to shift.
Returns:
A new Bignum whose value is this Bignum / 2^shiftBits
public Bignum shiftLeft(int shiftBits)
Returns a new Bignum calculated by shifting this Bignum to the
left by the number of bits given in shiftBits.
Parameters:
shiftBits The number of bits to shift
Returns:
A new Bignum whose value is this Bignum * 2^shiftBits
The scale for result is always set to 0.
This behaviour is not compatible with specification.
==== Here is the minimized test demonstrating the bug ====
import java.lang.Bignum;
class java_lang_Bignum_shiftLeft {
public static void main(String args[]) {
Bignum b1=new Bignum("1.1");
Bignum b2=b1.shiftLeft(1);
Bignum b3=b1.shiftRight(1);
System.out.println(b2); //should print "2.2" but prints "22"
System.out.println(b3); //should print "0.5" but prints "5"
}
}
==== Here is the output of the test ====
22
5
==== Here is the part of java language specification ====
public Bignum shiftRight(int shiftBits)
Returns a new Bignum whose value is calculated by shifting this Bignum
to the left by the number of bits given in shiftBits
Parameters:
shiftBits - the number of bits to shift.
Returns:
A new Bignum whose value is this Bignum / 2^shiftBits
public Bignum shiftLeft(int shiftBits)
Returns a new Bignum calculated by shifting this Bignum to the
left by the number of bits given in shiftBits.
Parameters:
shiftBits The number of bits to shift
Returns:
A new Bignum whose value is this Bignum * 2^shiftBits