Replace loop with Long.numberOfTrailingZeros in BigDecimal(double val, MathContext mc) constructor
Raffaello Giulietti
raffaello.giulietti at oracle.com
Tue Dec 23 10:55:10 UTC 2025
Straightforward enhancement, straightforward review.
Thanks John Platts for the upcoming PR.
________________________________________
From: core-libs-dev <core-libs-dev-retn at openjdk.org> on behalf of Chen Liang <chen.l.liang at oracle.com>
Sent: Monday, December 22, 2025 17:09
To: John Platts; core-libs-dev at openjdk.org
Subject: Re: Replace loop with Long.numberOfTrailingZeros in BigDecimal(double val, MathContext mc) constructor
Hello, this appears to be a harmless cleanup that has no behavioral impact. Created https://bugs.openjdk.org/browse/JDK-8374202 for this.
Feel free to follow our guide at https://openjdk.org/guide/ to submit a PR on GitHub for this issue.
Regards,
Chen Liang
________________________________
From: core-libs-dev <core-libs-dev-retn at openjdk.org> on behalf of John Platts <john_platts at hotmail.com>
Sent: Sunday, December 21, 2025 6:02 PM
To: core-libs-dev at openjdk.org <core-libs-dev at openjdk.org>
Subject: Replace loop with Long.numberOfTrailingZeros in BigDecimal(double val, MathContext mc) constructor
The following loop in the java.math.BigDecimal(double val, MathContext mc) constructor is inefficient:
while ((significand & 1) == 0) { // i.e., significand is even
significand >>= 1;
exponent++;
}
Here is a more efficient alternative using Long.numberOfTrailingZeros:
final int numOfTrailingZerosInSignificand =
Long.numberOfTrailingZeros(significand);
significand >>= numOfTrailingZerosInSignificand;
exponent += numOfTrailingZerosInSignificand;
More information about the core-libs-dev
mailing list