1 /*
2 * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
4578
4579 /**
4580 * Returns the specified int of the little-endian two's complement
4581 * representation (int 0 is the least significant). The int number can
4582 * be arbitrarily high (values are logically preceded by infinitely many
4583 * sign ints).
4584 */
4585 private int getInt(int n) {
4586 if (n < 0)
4587 return 0;
4588 if (n >= mag.length)
4589 return signInt();
4590
4591 int magInt = mag[mag.length-n-1];
4592
4593 return (signum >= 0 ? magInt :
4594 (n <= firstNonzeroIntNum() ? -magInt : ~magInt));
4595 }
4596
4597 /**
4598 * Returns the index of the int that contains the first nonzero int in the
4599 * little-endian binary representation of the magnitude (int 0 is the
4600 * least significant). If the magnitude is zero, return value is undefined.
4601 *
4602 * <p>Note: never used for a BigInteger with a magnitude of zero.
4603 * @see #getInt.
4604 */
4605 private int firstNonzeroIntNum() {
4606 int fn = firstNonzeroIntNumPlusTwo - 2;
4607 if (fn == -2) { // firstNonzeroIntNum not initialized yet
4608 // Search for the first nonzero int
4609 int i;
4610 int mlen = mag.length;
4611 for (i = mlen - 1; i >= 0 && mag[i] == 0; i--)
4612 ;
4613 fn = mlen - i - 1;
4614 firstNonzeroIntNumPlusTwo = fn + 2; // offset by two to initialize
4615 }
4616 return fn;
4617 }
4618
4619 /** use serialVersionUID from JDK 1.1. for interoperability */
4620 @java.io.Serial
4621 private static final long serialVersionUID = -8287574255936472291L;
4622
4623 /**
4624 * Serializable fields for BigInteger.
|
1 /*
2 * Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
4578
4579 /**
4580 * Returns the specified int of the little-endian two's complement
4581 * representation (int 0 is the least significant). The int number can
4582 * be arbitrarily high (values are logically preceded by infinitely many
4583 * sign ints).
4584 */
4585 private int getInt(int n) {
4586 if (n < 0)
4587 return 0;
4588 if (n >= mag.length)
4589 return signInt();
4590
4591 int magInt = mag[mag.length-n-1];
4592
4593 return (signum >= 0 ? magInt :
4594 (n <= firstNonzeroIntNum() ? -magInt : ~magInt));
4595 }
4596
4597 /**
4598 * Returns the index of the int that contains the first nonzero int in the
4599 * little-endian binary representation of the magnitude (int 0 is the
4600 * least significant). If the magnitude is zero, return value is undefined.
4601 *
4602 * <p>Note: never used for a BigInteger with a magnitude of zero.
4603 * @see #getInt.
4604 */
4605 private int firstNonzeroIntNum() {
4606 int fn = firstNonzeroIntNumPlusTwo - 2;
4607 if (fn == -2) { // firstNonzeroIntNum not initialized yet
4608 // Search for the first nonzero int
4609 int i;
4610 int mlen = mag.length;
4611 for (i = mlen - 1; i >= 0 && mag[i] == 0; i--)
4612 ;
4613 fn = mlen - i - 1;
4614 firstNonzeroIntNumPlusTwo = fn + 2; // offset by two to initialize
4615 }
4616 return fn;
4617 }
4618
4619 /** use serialVersionUID from JDK 1.1. for interoperability */
4620 @java.io.Serial
4621 private static final long serialVersionUID = -8287574255936472291L;
4622
4623 /**
4624 * Serializable fields for BigInteger.
|