1. /*
  2. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. /*
  6. * @(#)SignedMutableBigInteger.java 1.9 03/12/19
  7. */
  8. package java.math;
  9. /**
  10. * A class used to represent multiprecision integers that makes efficient
  11. * use of allocated space by allowing a number to occupy only part of
  12. * an array so that the arrays do not have to be reallocated as often.
  13. * When performing an operation with many iterations the array used to
  14. * hold a number is only increased when necessary and does not have to
  15. * be the same size as the number it represents. A mutable number allows
  16. * calculations to occur on the same number without having to create
  17. * a new number for every step of the calculation as occurs with
  18. * BigIntegers.
  19. *
  20. * Note that SignedMutableBigIntegers only support signed addition and
  21. * subtraction. All other operations occur as with MutableBigIntegers.
  22. *
  23. * @see BigInteger
  24. * @version 1.9, 12/19/03
  25. * @author Michael McCloskey
  26. * @since 1.3
  27. */
  28. class SignedMutableBigInteger extends MutableBigInteger {
  29. /**
  30. * The sign of this MutableBigInteger.
  31. */
  32. int sign = 1;
  33. // Constructors
  34. /**
  35. * The default constructor. An empty MutableBigInteger is created with
  36. * a one word capacity.
  37. */
  38. SignedMutableBigInteger() {
  39. super();
  40. }
  41. /**
  42. * Construct a new MutableBigInteger with a magnitude specified by
  43. * the int val.
  44. */
  45. SignedMutableBigInteger(int val) {
  46. super(val);
  47. }
  48. /**
  49. * Construct a new MutableBigInteger with a magnitude equal to the
  50. * specified MutableBigInteger.
  51. */
  52. SignedMutableBigInteger(MutableBigInteger val) {
  53. super(val);
  54. }
  55. // Arithmetic Operations
  56. /**
  57. * Signed addition built upon unsigned add and subtract.
  58. */
  59. void signedAdd(SignedMutableBigInteger addend) {
  60. if (sign == addend.sign)
  61. add(addend);
  62. else
  63. sign = sign * subtract(addend);
  64. }
  65. /**
  66. * Signed addition built upon unsigned add and subtract.
  67. */
  68. void signedAdd(MutableBigInteger addend) {
  69. if (sign == 1)
  70. add(addend);
  71. else
  72. sign = sign * subtract(addend);
  73. }
  74. /**
  75. * Signed subtraction built upon unsigned add and subtract.
  76. */
  77. void signedSubtract(SignedMutableBigInteger addend) {
  78. if (sign == addend.sign)
  79. sign = sign * subtract(addend);
  80. else
  81. add(addend);
  82. }
  83. /**
  84. * Signed subtraction built upon unsigned add and subtract.
  85. */
  86. void signedSubtract(MutableBigInteger addend) {
  87. if (sign == 1)
  88. sign = sign * subtract(addend);
  89. else
  90. add(addend);
  91. if (intLen == 0)
  92. sign = 1;
  93. }
  94. /**
  95. * Print out the first intLen ints of this MutableBigInteger's value
  96. * array starting at offset.
  97. */
  98. public String toString() {
  99. BigInteger b = new BigInteger(this, sign);
  100. return
  101. b.toString();
  102. }
  103. }