📄 long.java
字号:
* * @return the float value */ public float floatValue() { return value; } /** * Return the value of this <code>Long</code> as a <code>double</code>. * * @return the double value */ public double doubleValue() { return value; } /** * Converts the <code>Long</code> value to a <code>String</code> and * assumes a radix of 10. * * @return the <code>String</code> representation */ public String toString() { return toString(value, 10); } /** * Return a hashcode representing this Object. <code>Long</code>'s hash * code is calculated by <code>(int) (value ^ (value >> 32))</code>. * * @return this Object's hash code */ public int hashCode() { return (int) (value ^ (value >>> 32)); } /** * Returns <code>true</code> if <code>obj</code> is an instance of * <code>Long</code> and represents the same long value. * * @param obj the object to compare * @return whether these Objects are semantically equal */ public boolean equals(Object obj) { return obj instanceof Long && value == ((Long) obj).value; } /** * Get the specified system property as a <code>Long</code>. The * <code>decode()</code> method will be used to interpret the value of * the property. * * @param nm the name of the system property * @return the system property as a <code>Long</code>, or null if the * property is not found or cannot be decoded * @throws SecurityException if accessing the system property is forbidden * @see System#getProperty(String) * @see #decode(String) */ public static Long getLong(String nm) { return getLong(nm, null); } /** * Get the specified system property as a <code>Long</code>, or use a * default <code>long</code> value if the property is not found or is not * decodable. The <code>decode()</code> method will be used to interpret * the value of the property. * * @param nm the name of the system property * @param val the default value * @return the value of the system property, or the default * @throws SecurityException if accessing the system property is forbidden * @see System#getProperty(String) * @see #decode(String) */ public static Long getLong(String nm, long val) { Long result = getLong(nm, null); return result == null ? new Long(val) : result; } /** * Get the specified system property as a <code>Long</code>, or use a * default <code>Long</code> value if the property is not found or is * not decodable. The <code>decode()</code> method will be used to * interpret the value of the property. * * @param nm the name of the system property * @param def the default value * @return the value of the system property, or the default * @throws SecurityException if accessing the system property is forbidden * @see System#getProperty(String) * @see #decode(String) */ public static Long getLong(String nm, Long def) { if (nm == null || "".equals(nm)) return def; nm = System.getProperty(nm); if (nm == null) return def; try { return decode(nm); } catch (NumberFormatException e) { return def; } } /** * Compare two Longs numerically by comparing their <code>long</code> * values. The result is positive if the first is greater, negative if the * second is greater, and 0 if the two are equal. * * @param l the Long to compare * @return the comparison * @since 1.2 */ public int compareTo(Long l) { if (value == l.value) return 0; // Returns just -1 or 1 on inequality; doing math might overflow the long. return value > l.value ? 1 : -1; } /** * Behaves like <code>compareTo(Long)</code> unless the Object * is not a <code>Long</code>. * * @param o the object to compare * @return the comparison * @throws ClassCastException if the argument is not a <code>Long</code> * @see #compareTo(Long) * @see Comparable * @since 1.2 */ public int compareTo(Object o) { return compareTo((Long) o); } /** * Return the number of bits set in x. * @param x value to examine * @since 1.5 */ public static int bitCount(long x) { // Successively collapse alternating bit groups into a sum. x = ((x >> 1) & 0x5555555555555555L) + (x & 0x5555555555555555L); x = ((x >> 2) & 0x3333333333333333L) + (x & 0x3333333333333333L); int v = (int) ((x >>> 32) + x); v = ((v >> 4) & 0x0f0f0f0f) + (v & 0x0f0f0f0f); v = ((v >> 8) & 0x00ff00ff) + (v & 0x00ff00ff); return ((v >> 16) & 0x0000ffff) + (v & 0x0000ffff); } /** * Rotate x to the left by distance bits. * @param x the value to rotate * @param distance the number of bits by which to rotate * @since 1.5 */ public static long rotateLeft(long x, int distance) { // This trick works because the shift operators implicitly mask // the shift count. return (x << distance) | (x >>> - distance); } /** * Rotate x to the right by distance bits. * @param x the value to rotate * @param distance the number of bits by which to rotate * @since 1.5 */ public static long rotateRight(long x, int distance) { // This trick works because the shift operators implicitly mask // the shift count. return (x << - distance) | (x >>> distance); } /** * Find the highest set bit in value, and return a new value * with only that bit set. * @param value the value to examine * @since 1.5 */ public static long highestOneBit(long value) { value |= value >>> 1; value |= value >>> 2; value |= value >>> 4; value |= value >>> 8; value |= value >>> 16; value |= value >>> 32; return value ^ (value >>> 1); } /** * Return the number of leading zeros in value. * @param value the value to examine * @since 1.5 */ public static int numberOfLeadingZeros(long value) { value |= value >>> 1; value |= value >>> 2; value |= value >>> 4; value |= value >>> 8; value |= value >>> 16; value |= value >>> 32; return bitCount(~value); } /** * Find the lowest set bit in value, and return a new value * with only that bit set. * @param value the value to examine * @since 1.5 */ public static long lowestOneBit(long value) { // Classic assembly trick. return value & - value; } /** * Find the number of trailing zeros in value. * @param value the value to examine * @since 1.5 */ public static int numberOfTrailingZeros(long value) { return bitCount((value & -value) - 1); } /** * Return 1 if x is positive, -1 if it is negative, and 0 if it is * zero. * @param x the value to examine * @since 1.5 */ public static int signum(long x) { return x < 0 ? -1 : (x > 0 ? 1 : 0); } /** * Reverse the bytes in val. * @since 1.5 */ public static long reverseBytes(long val) { int hi = Integer.reverseBytes((int) val); int lo = Integer.reverseBytes((int) (val >>> 32)); return (((long) hi) << 32) | lo; } /** * Reverse the bits in val. * @since 1.5 */ public static long reverse(long val) { long hi = Integer.reverse((int) val) & 0xffffffffL; long lo = Integer.reverse((int) (val >>> 32)) & 0xffffffffL; return (hi << 32) | lo; } /** * Helper for converting unsigned numbers to String. * * @param num the number * @param exp log2(digit) (ie. 1, 3, or 4 for binary, oct, hex) */ private static String toUnsignedString(long num, int exp) { // Use the Integer toUnsignedString for efficiency if possible. // If NUM<0 then this particular optimization doesn't work // properly. if (num >= 0 && (int) num == num) return Integer.toUnsignedString((int) num, exp); // Use an array large enough for a binary number. int mask = (1 << exp) - 1; char[] buffer = new char[64]; int i = 64; do { buffer[--i] = digits[(int) num & mask]; num >>>= exp; } while (num != 0); // Package constructor avoids an array copy. return new String(buffer, i, 64 - i, true); } /** * Helper for parsing longs. * * @param str the string to parse * @param radix the radix to use, must be 10 if decode is true * @param decode if called from decode * @return the parsed long value * @throws NumberFormatException if there is an error * @throws NullPointerException if decode is true and str is null * @see #parseLong(String, int) * @see #decode(String) */ private static long parseLong(String str, int radix, boolean decode) { if (! decode && str == null) throw new NumberFormatException(); int index = 0; int len = str.length(); boolean isNeg = false; if (len == 0) throw new NumberFormatException(); int ch = str.charAt(index); if (ch == '-') { if (len == 1) throw new NumberFormatException(); isNeg = true; ch = str.charAt(++index); } if (decode) { if (ch == '0') { if (++index == len) return 0; if ((str.charAt(index) & ~('x' ^ 'X')) == 'X') { radix = 16; index++; } else radix = 8; } else if (ch == '#') { radix = 16; index++; } } if (index == len) throw new NumberFormatException(); long max = MAX_VALUE / radix; // We can't directly write `max = (MAX_VALUE + 1) / radix'. // So instead we fake it. if (isNeg && MAX_VALUE % radix == radix - 1) ++max; long val = 0; while (index < len) { if (val < 0 || val > max) throw new NumberFormatException(); ch = Character.digit(str.charAt(index++), radix); val = val * radix + ch; if (ch < 0 || (val < 0 && (! isNeg || val != MIN_VALUE))) throw new NumberFormatException(); } return isNeg ? -val : val; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -