Replace loop with Long.numberOfTrailingZeros in BigDecimal(double val, MathContext mc) constructor

Chen Liang chen.l.liang at oracle.com
Mon Dec 22 16:09:21 UTC 2025


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;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20251222/e3c39859/attachment.htm>


More information about the core-libs-dev mailing list