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
23 * questions.
24 */
25
26 package java.lang;
27
28 import java.lang.annotation.Native;
29 import java.math.*;
30 import java.util.Objects;
31
32
33 /**
34 * The {@code Long} class wraps a value of the primitive type {@code
35 * long} in an object. An object of type {@code Long} contains a
36 * single field whose type is {@code long}.
37 *
38 * <p> In addition, this class provides several methods for converting
39 * a {@code long} to a {@code String} and a {@code String} to a {@code
40 * long}, as well as other constants and methods useful when dealing
41 * with a {@code long}.
42 *
43 * <p>Implementation note: The implementations of the "bit twiddling"
44 * methods (such as {@link #highestOneBit(long) highestOneBit} and
45 * {@link #numberOfTrailingZeros(long) numberOfTrailingZeros}) are
46 * based on material from Henry S. Warren, Jr.'s <i>Hacker's
47 * Delight</i>, (Addison Wesley, 2002).
48 *
49 * @author Lee Boynton
50 * @author Arthur van Hoff
1057 }
1058
1059 /**
1060 * Returns a {@code Long} instance representing the specified
1061 * {@code long} value.
1062 * If a new {@code Long} instance is not required, this method
1063 * should generally be used in preference to the constructor
1064 * {@link #Long(long)}, as this method is likely to yield
1065 * significantly better space and time performance by caching
1066 * frequently requested values.
1067 *
1068 * Note that unlike the {@linkplain Integer#valueOf(int)
1069 * corresponding method} in the {@code Integer} class, this method
1070 * is <em>not</em> required to cache values within a particular
1071 * range.
1072 *
1073 * @param l a long value.
1074 * @return a {@code Long} instance representing {@code l}.
1075 * @since 1.5
1076 */
1077 public static Long valueOf(long l) {
1078 final int offset = 128;
1079 if (l >= -128 && l <= 127) { // will cache
1080 return LongCache.cache[(int)l + offset];
1081 }
1082 return new Long(l);
1083 }
1084
1085 /**
1086 * Decodes a {@code String} into a {@code Long}.
1087 * Accepts decimal, hexadecimal, and octal numbers given by the
1088 * following grammar:
1089 *
1090 * <blockquote>
1091 * <dl>
1092 * <dt><i>DecodableString:</i>
1093 * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
1094 * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
1095 * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
1096 * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
1221 * a narrowing primitive conversion.
1222 * @jls 5.1.3 Narrowing Primitive Conversions
1223 */
1224 public short shortValue() {
1225 return (short)value;
1226 }
1227
1228 /**
1229 * Returns the value of this {@code Long} as an {@code int} after
1230 * a narrowing primitive conversion.
1231 * @jls 5.1.3 Narrowing Primitive Conversions
1232 */
1233 public int intValue() {
1234 return (int)value;
1235 }
1236
1237 /**
1238 * Returns the value of this {@code Long} as a
1239 * {@code long} value.
1240 */
1241 public long longValue() {
1242 return value;
1243 }
1244
1245 /**
1246 * Returns the value of this {@code Long} as a {@code float} after
1247 * a widening primitive conversion.
1248 * @jls 5.1.2 Widening Primitive Conversions
1249 */
1250 public float floatValue() {
1251 return (float)value;
1252 }
1253
1254 /**
1255 * Returns the value of this {@code Long} as a {@code double}
1256 * after a widening primitive conversion.
1257 * @jls 5.1.2 Widening Primitive Conversions
1258 */
1259 public double doubleValue() {
1260 return (double)value;
1638 * Returns the number of zero bits preceding the highest-order
1639 * ("leftmost") one-bit in the two's complement binary representation
1640 * of the specified {@code long} value. Returns 64 if the
1641 * specified value has no one-bits in its two's complement representation,
1642 * in other words if it is equal to zero.
1643 *
1644 * <p>Note that this method is closely related to the logarithm base 2.
1645 * For all positive {@code long} values x:
1646 * <ul>
1647 * <li>floor(log<sub>2</sub>(x)) = {@code 63 - numberOfLeadingZeros(x)}
1648 * <li>ceil(log<sub>2</sub>(x)) = {@code 64 - numberOfLeadingZeros(x - 1)}
1649 * </ul>
1650 *
1651 * @param i the value whose number of leading zeros is to be computed
1652 * @return the number of zero bits preceding the highest-order
1653 * ("leftmost") one-bit in the two's complement binary representation
1654 * of the specified {@code long} value, or 64 if the value
1655 * is equal to zero.
1656 * @since 1.5
1657 */
1658 public static int numberOfLeadingZeros(long i) {
1659 // HD, Figure 5-6
1660 if (i == 0)
1661 return 64;
1662 int n = 1;
1663 int x = (int)(i >>> 32);
1664 if (x == 0) { n += 32; x = (int)i; }
1665 if (x >>> 16 == 0) { n += 16; x <<= 16; }
1666 if (x >>> 24 == 0) { n += 8; x <<= 8; }
1667 if (x >>> 28 == 0) { n += 4; x <<= 4; }
1668 if (x >>> 30 == 0) { n += 2; x <<= 2; }
1669 n -= x >>> 31;
1670 return n;
1671 }
1672
1673 /**
1674 * Returns the number of zero bits following the lowest-order ("rightmost")
1675 * one-bit in the two's complement binary representation of the specified
1676 * {@code long} value. Returns 64 if the specified value has no
1677 * one-bits in its two's complement representation, in other words if it is
1678 * equal to zero.
1679 *
1680 * @param i the value whose number of trailing zeros is to be computed
1681 * @return the number of zero bits following the lowest-order ("rightmost")
1682 * one-bit in the two's complement binary representation of the
1683 * specified {@code long} value, or 64 if the value is equal
1684 * to zero.
1685 * @since 1.5
1686 */
1687 public static int numberOfTrailingZeros(long i) {
1688 // HD, Figure 5-14
1689 int x, y;
1690 if (i == 0) return 64;
1691 int n = 63;
1692 y = (int)i; if (y != 0) { n = n -32; x = y; } else x = (int)(i>>>32);
1693 y = x <<16; if (y != 0) { n = n -16; x = y; }
1694 y = x << 8; if (y != 0) { n = n - 8; x = y; }
1695 y = x << 4; if (y != 0) { n = n - 4; x = y; }
1696 y = x << 2; if (y != 0) { n = n - 2; x = y; }
1697 return n - ((x << 1) >>> 31);
1698 }
1699
1700 /**
1701 * Returns the number of one-bits in the two's complement binary
1702 * representation of the specified {@code long} value. This function is
1703 * sometimes referred to as the <i>population count</i>.
1704 *
1705 * @param i the value whose bits are to be counted
1706 * @return the number of one-bits in the two's complement binary
1707 * representation of the specified {@code long} value.
1708 * @since 1.5
1709 */
1710 public static int bitCount(long i) {
1711 // HD, Figure 5-2
1712 i = i - ((i >>> 1) & 0x5555555555555555L);
1713 i = (i & 0x3333333333333333L) + ((i >>> 2) & 0x3333333333333333L);
1714 i = (i + (i >>> 4)) & 0x0f0f0f0f0f0f0f0fL;
1715 i = i + (i >>> 8);
1716 i = i + (i >>> 16);
1717 i = i + (i >>> 32);
1718 return (int)i & 0x7f;
1719 }
1720
1721 /**
1722 * Returns the value obtained by rotating the two's complement binary
1723 * representation of the specified {@code long} value left by the
1724 * specified number of bits. (Bits shifted out of the left hand, or
1725 * high-order, side reenter on the right, or low-order.)
1726 *
1727 * <p>Note that left rotation with a negative distance is equivalent to
1728 * right rotation: {@code rotateLeft(val, -distance) == rotateRight(val,
1729 * distance)}. Note also that rotation by any multiple of 64 is a
1793 * specified value is zero; and 1 if the specified value is positive.)
1794 *
1795 * @param i the value whose signum is to be computed
1796 * @return the signum function of the specified {@code long} value.
1797 * @since 1.5
1798 */
1799 public static int signum(long i) {
1800 // HD, Section 2-7
1801 return (int) ((i >> 63) | (-i >>> 63));
1802 }
1803
1804 /**
1805 * Returns the value obtained by reversing the order of the bytes in the
1806 * two's complement representation of the specified {@code long} value.
1807 *
1808 * @param i the value whose bytes are to be reversed
1809 * @return the value obtained by reversing the bytes in the specified
1810 * {@code long} value.
1811 * @since 1.5
1812 */
1813 public static long reverseBytes(long i) {
1814 i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL;
1815 return (i << 48) | ((i & 0xffff0000L) << 16) |
1816 ((i >>> 16) & 0xffff0000L) | (i >>> 48);
1817 }
1818
1819 /**
1820 * Adds two {@code long} values together as per the + operator.
1821 *
1822 * @param a the first operand
1823 * @param b the second operand
1824 * @return the sum of {@code a} and {@code b}
1825 * @see java.util.function.BinaryOperator
1826 * @since 1.8
1827 */
1828 public static long sum(long a, long b) {
1829 return a + b;
1830 }
1831
1832 /**
|
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
23 * questions.
24 */
25
26 package java.lang;
27
28 import java.lang.annotation.Native;
29 import java.math.*;
30 import java.util.Objects;
31 import jdk.internal.HotSpotIntrinsicCandidate;
32
33
34 /**
35 * The {@code Long} class wraps a value of the primitive type {@code
36 * long} in an object. An object of type {@code Long} contains a
37 * single field whose type is {@code long}.
38 *
39 * <p> In addition, this class provides several methods for converting
40 * a {@code long} to a {@code String} and a {@code String} to a {@code
41 * long}, as well as other constants and methods useful when dealing
42 * with a {@code long}.
43 *
44 * <p>Implementation note: The implementations of the "bit twiddling"
45 * methods (such as {@link #highestOneBit(long) highestOneBit} and
46 * {@link #numberOfTrailingZeros(long) numberOfTrailingZeros}) are
47 * based on material from Henry S. Warren, Jr.'s <i>Hacker's
48 * Delight</i>, (Addison Wesley, 2002).
49 *
50 * @author Lee Boynton
51 * @author Arthur van Hoff
1058 }
1059
1060 /**
1061 * Returns a {@code Long} instance representing the specified
1062 * {@code long} value.
1063 * If a new {@code Long} instance is not required, this method
1064 * should generally be used in preference to the constructor
1065 * {@link #Long(long)}, as this method is likely to yield
1066 * significantly better space and time performance by caching
1067 * frequently requested values.
1068 *
1069 * Note that unlike the {@linkplain Integer#valueOf(int)
1070 * corresponding method} in the {@code Integer} class, this method
1071 * is <em>not</em> required to cache values within a particular
1072 * range.
1073 *
1074 * @param l a long value.
1075 * @return a {@code Long} instance representing {@code l}.
1076 * @since 1.5
1077 */
1078 @HotSpotIntrinsicCandidate
1079 public static Long valueOf(long l) {
1080 final int offset = 128;
1081 if (l >= -128 && l <= 127) { // will cache
1082 return LongCache.cache[(int)l + offset];
1083 }
1084 return new Long(l);
1085 }
1086
1087 /**
1088 * Decodes a {@code String} into a {@code Long}.
1089 * Accepts decimal, hexadecimal, and octal numbers given by the
1090 * following grammar:
1091 *
1092 * <blockquote>
1093 * <dl>
1094 * <dt><i>DecodableString:</i>
1095 * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
1096 * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
1097 * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
1098 * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
1223 * a narrowing primitive conversion.
1224 * @jls 5.1.3 Narrowing Primitive Conversions
1225 */
1226 public short shortValue() {
1227 return (short)value;
1228 }
1229
1230 /**
1231 * Returns the value of this {@code Long} as an {@code int} after
1232 * a narrowing primitive conversion.
1233 * @jls 5.1.3 Narrowing Primitive Conversions
1234 */
1235 public int intValue() {
1236 return (int)value;
1237 }
1238
1239 /**
1240 * Returns the value of this {@code Long} as a
1241 * {@code long} value.
1242 */
1243 @HotSpotIntrinsicCandidate
1244 public long longValue() {
1245 return value;
1246 }
1247
1248 /**
1249 * Returns the value of this {@code Long} as a {@code float} after
1250 * a widening primitive conversion.
1251 * @jls 5.1.2 Widening Primitive Conversions
1252 */
1253 public float floatValue() {
1254 return (float)value;
1255 }
1256
1257 /**
1258 * Returns the value of this {@code Long} as a {@code double}
1259 * after a widening primitive conversion.
1260 * @jls 5.1.2 Widening Primitive Conversions
1261 */
1262 public double doubleValue() {
1263 return (double)value;
1641 * Returns the number of zero bits preceding the highest-order
1642 * ("leftmost") one-bit in the two's complement binary representation
1643 * of the specified {@code long} value. Returns 64 if the
1644 * specified value has no one-bits in its two's complement representation,
1645 * in other words if it is equal to zero.
1646 *
1647 * <p>Note that this method is closely related to the logarithm base 2.
1648 * For all positive {@code long} values x:
1649 * <ul>
1650 * <li>floor(log<sub>2</sub>(x)) = {@code 63 - numberOfLeadingZeros(x)}
1651 * <li>ceil(log<sub>2</sub>(x)) = {@code 64 - numberOfLeadingZeros(x - 1)}
1652 * </ul>
1653 *
1654 * @param i the value whose number of leading zeros is to be computed
1655 * @return the number of zero bits preceding the highest-order
1656 * ("leftmost") one-bit in the two's complement binary representation
1657 * of the specified {@code long} value, or 64 if the value
1658 * is equal to zero.
1659 * @since 1.5
1660 */
1661 @HotSpotIntrinsicCandidate
1662 public static int numberOfLeadingZeros(long i) {
1663 // HD, Figure 5-6
1664 if (i == 0)
1665 return 64;
1666 int n = 1;
1667 int x = (int)(i >>> 32);
1668 if (x == 0) { n += 32; x = (int)i; }
1669 if (x >>> 16 == 0) { n += 16; x <<= 16; }
1670 if (x >>> 24 == 0) { n += 8; x <<= 8; }
1671 if (x >>> 28 == 0) { n += 4; x <<= 4; }
1672 if (x >>> 30 == 0) { n += 2; x <<= 2; }
1673 n -= x >>> 31;
1674 return n;
1675 }
1676
1677 /**
1678 * Returns the number of zero bits following the lowest-order ("rightmost")
1679 * one-bit in the two's complement binary representation of the specified
1680 * {@code long} value. Returns 64 if the specified value has no
1681 * one-bits in its two's complement representation, in other words if it is
1682 * equal to zero.
1683 *
1684 * @param i the value whose number of trailing zeros is to be computed
1685 * @return the number of zero bits following the lowest-order ("rightmost")
1686 * one-bit in the two's complement binary representation of the
1687 * specified {@code long} value, or 64 if the value is equal
1688 * to zero.
1689 * @since 1.5
1690 */
1691 @HotSpotIntrinsicCandidate
1692 public static int numberOfTrailingZeros(long i) {
1693 // HD, Figure 5-14
1694 int x, y;
1695 if (i == 0) return 64;
1696 int n = 63;
1697 y = (int)i; if (y != 0) { n = n -32; x = y; } else x = (int)(i>>>32);
1698 y = x <<16; if (y != 0) { n = n -16; x = y; }
1699 y = x << 8; if (y != 0) { n = n - 8; x = y; }
1700 y = x << 4; if (y != 0) { n = n - 4; x = y; }
1701 y = x << 2; if (y != 0) { n = n - 2; x = y; }
1702 return n - ((x << 1) >>> 31);
1703 }
1704
1705 /**
1706 * Returns the number of one-bits in the two's complement binary
1707 * representation of the specified {@code long} value. This function is
1708 * sometimes referred to as the <i>population count</i>.
1709 *
1710 * @param i the value whose bits are to be counted
1711 * @return the number of one-bits in the two's complement binary
1712 * representation of the specified {@code long} value.
1713 * @since 1.5
1714 */
1715 @HotSpotIntrinsicCandidate
1716 public static int bitCount(long i) {
1717 // HD, Figure 5-2
1718 i = i - ((i >>> 1) & 0x5555555555555555L);
1719 i = (i & 0x3333333333333333L) + ((i >>> 2) & 0x3333333333333333L);
1720 i = (i + (i >>> 4)) & 0x0f0f0f0f0f0f0f0fL;
1721 i = i + (i >>> 8);
1722 i = i + (i >>> 16);
1723 i = i + (i >>> 32);
1724 return (int)i & 0x7f;
1725 }
1726
1727 /**
1728 * Returns the value obtained by rotating the two's complement binary
1729 * representation of the specified {@code long} value left by the
1730 * specified number of bits. (Bits shifted out of the left hand, or
1731 * high-order, side reenter on the right, or low-order.)
1732 *
1733 * <p>Note that left rotation with a negative distance is equivalent to
1734 * right rotation: {@code rotateLeft(val, -distance) == rotateRight(val,
1735 * distance)}. Note also that rotation by any multiple of 64 is a
1799 * specified value is zero; and 1 if the specified value is positive.)
1800 *
1801 * @param i the value whose signum is to be computed
1802 * @return the signum function of the specified {@code long} value.
1803 * @since 1.5
1804 */
1805 public static int signum(long i) {
1806 // HD, Section 2-7
1807 return (int) ((i >> 63) | (-i >>> 63));
1808 }
1809
1810 /**
1811 * Returns the value obtained by reversing the order of the bytes in the
1812 * two's complement representation of the specified {@code long} value.
1813 *
1814 * @param i the value whose bytes are to be reversed
1815 * @return the value obtained by reversing the bytes in the specified
1816 * {@code long} value.
1817 * @since 1.5
1818 */
1819 @HotSpotIntrinsicCandidate
1820 public static long reverseBytes(long i) {
1821 i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL;
1822 return (i << 48) | ((i & 0xffff0000L) << 16) |
1823 ((i >>> 16) & 0xffff0000L) | (i >>> 48);
1824 }
1825
1826 /**
1827 * Adds two {@code long} values together as per the + operator.
1828 *
1829 * @param a the first operand
1830 * @param b the second operand
1831 * @return the sum of {@code a} and {@code b}
1832 * @see java.util.function.BinaryOperator
1833 * @since 1.8
1834 */
1835 public static long sum(long a, long b) {
1836 return a + b;
1837 }
1838
1839 /**
|