jaroslav@67: /* jaroslav@67: * Copyright (c) 1994, 2009, Oracle and/or its affiliates. All rights reserved. jaroslav@67: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@67: * jaroslav@67: * This code is free software; you can redistribute it and/or modify it jaroslav@67: * under the terms of the GNU General Public License version 2 only, as jaroslav@67: * published by the Free Software Foundation. Oracle designates this jaroslav@67: * particular file as subject to the "Classpath" exception as provided jaroslav@67: * by Oracle in the LICENSE file that accompanied this code. jaroslav@67: * jaroslav@67: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@67: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@67: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@67: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@67: * accompanied this code). jaroslav@67: * jaroslav@67: * You should have received a copy of the GNU General Public License version jaroslav@67: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@67: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@67: * jaroslav@67: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@67: * or visit www.oracle.com if you need additional information or have any jaroslav@67: * questions. jaroslav@67: */ jaroslav@67: jaroslav@67: package java.lang; jaroslav@67: jaroslav@179: import org.apidesign.bck2brwsr.core.JavaScriptBody; jaroslav@179: jaroslav@67: /** jaroslav@67: * The {@code Long} class wraps a value of the primitive type {@code jaroslav@67: * long} in an object. An object of type {@code Long} contains a jaroslav@67: * single field whose type is {@code long}. jaroslav@67: * jaroslav@67: *

In addition, this class provides several methods for converting jaroslav@67: * a {@code long} to a {@code String} and a {@code String} to a {@code jaroslav@67: * long}, as well as other constants and methods useful when dealing jaroslav@67: * with a {@code long}. jaroslav@67: * jaroslav@67: *

Implementation note: The implementations of the "bit twiddling" jaroslav@67: * methods (such as {@link #highestOneBit(long) highestOneBit} and jaroslav@67: * {@link #numberOfTrailingZeros(long) numberOfTrailingZeros}) are jaroslav@67: * based on material from Henry S. Warren, Jr.'s Hacker's jaroslav@67: * Delight, (Addison Wesley, 2002). jaroslav@67: * jaroslav@67: * @author Lee Boynton jaroslav@67: * @author Arthur van Hoff jaroslav@67: * @author Josh Bloch jaroslav@67: * @author Joseph D. Darcy jaroslav@67: * @since JDK1.0 jaroslav@67: */ jaroslav@67: public final class Long extends Number implements Comparable { jaroslav@67: /** jaroslav@67: * A constant holding the minimum value a {@code long} can jaroslav@67: * have, -263. jaroslav@67: */ jaroslav@67: public static final long MIN_VALUE = 0x8000000000000000L; jaroslav@67: jaroslav@67: /** jaroslav@67: * A constant holding the maximum value a {@code long} can jaroslav@67: * have, 263-1. jaroslav@67: */ jaroslav@67: public static final long MAX_VALUE = 0x7fffffffffffffffL; jaroslav@67: jaroslav@67: /** jaroslav@67: * The {@code Class} instance representing the primitive type jaroslav@67: * {@code long}. jaroslav@67: * jaroslav@67: * @since JDK1.1 jaroslav@67: */ jaroslav@67: public static final Class TYPE = (Class) Class.getPrimitiveClass("long"); jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a string representation of the first argument in the jaroslav@67: * radix specified by the second argument. jaroslav@67: * jaroslav@67: *

If the radix is smaller than {@code Character.MIN_RADIX} jaroslav@67: * or larger than {@code Character.MAX_RADIX}, then the radix jaroslav@67: * {@code 10} is used instead. jaroslav@67: * jaroslav@67: *

If the first argument is negative, the first element of the jaroslav@67: * result is the ASCII minus sign {@code '-'} jaroslav@67: * ('\u002d'). If the first argument is not jaroslav@67: * negative, no sign character appears in the result. jaroslav@67: * jaroslav@67: *

The remaining characters of the result represent the magnitude jaroslav@67: * of the first argument. If the magnitude is zero, it is jaroslav@67: * represented by a single zero character {@code '0'} jaroslav@67: * ('\u0030'); otherwise, the first character of jaroslav@67: * the representation of the magnitude will not be the zero jaroslav@67: * character. The following ASCII characters are used as digits: jaroslav@67: * jaroslav@67: *

jaroslav@67: * {@code 0123456789abcdefghijklmnopqrstuvwxyz} jaroslav@67: *
jaroslav@67: * jaroslav@67: * These are '\u0030' through jaroslav@67: * '\u0039' and '\u0061' through jaroslav@67: * '\u007a'. If {@code radix} is jaroslav@67: * N, then the first N of these characters jaroslav@67: * are used as radix-N digits in the order shown. Thus, jaroslav@67: * the digits for hexadecimal (radix 16) are jaroslav@67: * {@code 0123456789abcdef}. If uppercase letters are jaroslav@67: * desired, the {@link java.lang.String#toUpperCase()} method may jaroslav@67: * be called on the result: jaroslav@67: * jaroslav@67: *
jaroslav@67: * {@code Long.toString(n, 16).toUpperCase()} jaroslav@67: *
jaroslav@67: * jaroslav@67: * @param i a {@code long} to be converted to a string. jaroslav@67: * @param radix the radix to use in the string representation. jaroslav@67: * @return a string representation of the argument in the specified radix. jaroslav@67: * @see java.lang.Character#MAX_RADIX jaroslav@67: * @see java.lang.Character#MIN_RADIX jaroslav@67: */ jaroslav@67: public static String toString(long i, int radix) { jaroslav@67: if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) jaroslav@67: radix = 10; jaroslav@67: if (radix == 10) jaroslav@67: return toString(i); jaroslav@67: char[] buf = new char[65]; jaroslav@67: int charPos = 64; jaroslav@67: boolean negative = (i < 0); jaroslav@67: jaroslav@67: if (!negative) { jaroslav@67: i = -i; jaroslav@67: } jaroslav@67: jaroslav@67: while (i <= -radix) { jaroslav@67: buf[charPos--] = Integer.digits[(int)(-(i % radix))]; jaroslav@67: i = i / radix; jaroslav@67: } jaroslav@67: buf[charPos] = Integer.digits[(int)(-i)]; jaroslav@67: jaroslav@67: if (negative) { jaroslav@67: buf[--charPos] = '-'; jaroslav@67: } jaroslav@67: jaroslav@67: return new String(buf, charPos, (65 - charPos)); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a string representation of the {@code long} jaroslav@67: * argument as an unsigned integer in base 16. jaroslav@67: * jaroslav@67: *

The unsigned {@code long} value is the argument plus jaroslav@67: * 264 if the argument is negative; otherwise, it is jaroslav@67: * equal to the argument. This value is converted to a string of jaroslav@67: * ASCII digits in hexadecimal (base 16) with no extra jaroslav@67: * leading {@code 0}s. If the unsigned magnitude is zero, it jaroslav@67: * is represented by a single zero character {@code '0'} jaroslav@67: * ('\u0030'); otherwise, the first character of jaroslav@67: * the representation of the unsigned magnitude will not be the jaroslav@67: * zero character. The following characters are used as jaroslav@67: * hexadecimal digits: jaroslav@67: * jaroslav@67: *

jaroslav@67: * {@code 0123456789abcdef} jaroslav@67: *
jaroslav@67: * jaroslav@67: * These are the characters '\u0030' through jaroslav@67: * '\u0039' and '\u0061' through jaroslav@67: * '\u0066'. If uppercase letters are desired, jaroslav@67: * the {@link java.lang.String#toUpperCase()} method may be called jaroslav@67: * on the result: jaroslav@67: * jaroslav@67: *
jaroslav@67: * {@code Long.toHexString(n).toUpperCase()} jaroslav@67: *
jaroslav@67: * jaroslav@67: * @param i a {@code long} to be converted to a string. jaroslav@67: * @return the string representation of the unsigned {@code long} jaroslav@67: * value represented by the argument in hexadecimal jaroslav@67: * (base 16). jaroslav@67: * @since JDK 1.0.2 jaroslav@67: */ jaroslav@67: public static String toHexString(long i) { jaroslav@67: return toUnsignedString(i, 4); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a string representation of the {@code long} jaroslav@67: * argument as an unsigned integer in base 8. jaroslav@67: * jaroslav@67: *

The unsigned {@code long} value is the argument plus jaroslav@67: * 264 if the argument is negative; otherwise, it is jaroslav@67: * equal to the argument. This value is converted to a string of jaroslav@67: * ASCII digits in octal (base 8) with no extra leading jaroslav@67: * {@code 0}s. jaroslav@67: * jaroslav@67: *

If the unsigned magnitude is zero, it is represented by a jaroslav@67: * single zero character {@code '0'} jaroslav@67: * ('\u0030'); otherwise, the first character of jaroslav@67: * the representation of the unsigned magnitude will not be the jaroslav@67: * zero character. The following characters are used as octal jaroslav@67: * digits: jaroslav@67: * jaroslav@67: *

jaroslav@67: * {@code 01234567} jaroslav@67: *
jaroslav@67: * jaroslav@67: * These are the characters '\u0030' through jaroslav@67: * '\u0037'. jaroslav@67: * jaroslav@67: * @param i a {@code long} to be converted to a string. jaroslav@67: * @return the string representation of the unsigned {@code long} jaroslav@67: * value represented by the argument in octal (base 8). jaroslav@67: * @since JDK 1.0.2 jaroslav@67: */ jaroslav@67: public static String toOctalString(long i) { jaroslav@67: return toUnsignedString(i, 3); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a string representation of the {@code long} jaroslav@67: * argument as an unsigned integer in base 2. jaroslav@67: * jaroslav@67: *

The unsigned {@code long} value is the argument plus jaroslav@67: * 264 if the argument is negative; otherwise, it is jaroslav@67: * equal to the argument. This value is converted to a string of jaroslav@67: * ASCII digits in binary (base 2) with no extra leading jaroslav@67: * {@code 0}s. If the unsigned magnitude is zero, it is jaroslav@67: * represented by a single zero character {@code '0'} jaroslav@67: * ('\u0030'); otherwise, the first character of jaroslav@67: * the representation of the unsigned magnitude will not be the jaroslav@67: * zero character. The characters {@code '0'} jaroslav@67: * ('\u0030') and {@code '1'} jaroslav@67: * ('\u0031') are used as binary digits. jaroslav@67: * jaroslav@67: * @param i a {@code long} to be converted to a string. jaroslav@67: * @return the string representation of the unsigned {@code long} jaroslav@67: * value represented by the argument in binary (base 2). jaroslav@67: * @since JDK 1.0.2 jaroslav@67: */ jaroslav@67: public static String toBinaryString(long i) { jaroslav@67: return toUnsignedString(i, 1); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Convert the integer to an unsigned number. jaroslav@67: */ jaroslav@67: private static String toUnsignedString(long i, int shift) { jaroslav@67: char[] buf = new char[64]; jaroslav@67: int charPos = 64; jaroslav@67: int radix = 1 << shift; jaroslav@67: long mask = radix - 1; jaroslav@67: do { jaroslav@67: buf[--charPos] = Integer.digits[(int)(i & mask)]; jaroslav@67: i >>>= shift; jaroslav@67: } while (i != 0); jaroslav@67: return new String(buf, charPos, (64 - charPos)); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a {@code String} object representing the specified jaroslav@67: * {@code long}. The argument is converted to signed decimal jaroslav@67: * representation and returned as a string, exactly as if the jaroslav@67: * argument and the radix 10 were given as arguments to the {@link jaroslav@67: * #toString(long, int)} method. jaroslav@67: * jaroslav@67: * @param i a {@code long} to be converted. jaroslav@67: * @return a string representation of the argument in base 10. jaroslav@67: */ jaroslav@179: @JavaScriptBody(args = "i", body = "return i.toString();") jaroslav@67: public static String toString(long i) { jaroslav@67: if (i == Long.MIN_VALUE) jaroslav@67: return "-9223372036854775808"; jaroslav@67: int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); jaroslav@67: char[] buf = new char[size]; jaroslav@67: getChars(i, size, buf); jaroslav@179: return new String(buf, 0, size); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Places characters representing the integer i into the jaroslav@67: * character array buf. The characters are placed into jaroslav@67: * the buffer backwards starting with the least significant jaroslav@67: * digit at the specified index (exclusive), and working jaroslav@67: * backwards from there. jaroslav@67: * jaroslav@67: * Will fail if i == Long.MIN_VALUE jaroslav@67: */ jaroslav@67: static void getChars(long i, int index, char[] buf) { jaroslav@67: long q; jaroslav@67: int r; jaroslav@67: int charPos = index; jaroslav@67: char sign = 0; jaroslav@67: jaroslav@67: if (i < 0) { jaroslav@67: sign = '-'; jaroslav@67: i = -i; jaroslav@67: } jaroslav@67: jaroslav@67: // Get 2 digits/iteration using longs until quotient fits into an int jaroslav@67: while (i > Integer.MAX_VALUE) { jaroslav@67: q = i / 100; jaroslav@67: // really: r = i - (q * 100); jaroslav@67: r = (int)(i - ((q << 6) + (q << 5) + (q << 2))); jaroslav@67: i = q; jaroslav@67: buf[--charPos] = Integer.DigitOnes[r]; jaroslav@67: buf[--charPos] = Integer.DigitTens[r]; jaroslav@67: } jaroslav@67: jaroslav@67: // Get 2 digits/iteration using ints jaroslav@67: int q2; jaroslav@67: int i2 = (int)i; jaroslav@67: while (i2 >= 65536) { jaroslav@67: q2 = i2 / 100; jaroslav@67: // really: r = i2 - (q * 100); jaroslav@67: r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2)); jaroslav@67: i2 = q2; jaroslav@67: buf[--charPos] = Integer.DigitOnes[r]; jaroslav@67: buf[--charPos] = Integer.DigitTens[r]; jaroslav@67: } jaroslav@67: jaroslav@67: // Fall thru to fast mode for smaller numbers jaroslav@67: // assert(i2 <= 65536, i2); jaroslav@67: for (;;) { jaroslav@67: q2 = (i2 * 52429) >>> (16+3); jaroslav@67: r = i2 - ((q2 << 3) + (q2 << 1)); // r = i2-(q2*10) ... jaroslav@67: buf[--charPos] = Integer.digits[r]; jaroslav@67: i2 = q2; jaroslav@67: if (i2 == 0) break; jaroslav@67: } jaroslav@67: if (sign != 0) { jaroslav@67: buf[--charPos] = sign; jaroslav@67: } jaroslav@67: } jaroslav@67: jaroslav@67: // Requires positive x jaroslav@67: static int stringSize(long x) { jaroslav@67: long p = 10; jaroslav@67: for (int i=1; i<19; i++) { jaroslav@67: if (x < p) jaroslav@67: return i; jaroslav@67: p = 10*p; jaroslav@67: } jaroslav@67: return 19; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Parses the string argument as a signed {@code long} in the jaroslav@67: * radix specified by the second argument. The characters in the jaroslav@67: * string must all be digits of the specified radix (as determined jaroslav@67: * by whether {@link java.lang.Character#digit(char, int)} returns jaroslav@67: * a nonnegative value), except that the first character may be an jaroslav@67: * ASCII minus sign {@code '-'} ('\u002D') to jaroslav@67: * indicate a negative value or an ASCII plus sign {@code '+'} jaroslav@67: * ('\u002B') to indicate a positive value. The jaroslav@67: * resulting {@code long} value is returned. jaroslav@67: * jaroslav@67: *

Note that neither the character {@code L} jaroslav@67: * ('\u004C') nor {@code l} jaroslav@67: * ('\u006C') is permitted to appear at the end jaroslav@67: * of the string as a type indicator, as would be permitted in jaroslav@67: * Java programming language source code - except that either jaroslav@67: * {@code L} or {@code l} may appear as a digit for a jaroslav@67: * radix greater than 22. jaroslav@67: * jaroslav@67: *

An exception of type {@code NumberFormatException} is jaroslav@67: * thrown if any of the following situations occurs: jaroslav@67: *

jaroslav@67: * jaroslav@67: *

Examples: jaroslav@67: *

jaroslav@67:      * parseLong("0", 10) returns 0L
jaroslav@67:      * parseLong("473", 10) returns 473L
jaroslav@67:      * parseLong("+42", 10) returns 42L
jaroslav@67:      * parseLong("-0", 10) returns 0L
jaroslav@67:      * parseLong("-FF", 16) returns -255L
jaroslav@67:      * parseLong("1100110", 2) returns 102L
jaroslav@67:      * parseLong("99", 8) throws a NumberFormatException
jaroslav@67:      * parseLong("Hazelnut", 10) throws a NumberFormatException
jaroslav@67:      * parseLong("Hazelnut", 36) returns 1356099454469L
jaroslav@67:      * 
jaroslav@67: * jaroslav@67: * @param s the {@code String} containing the jaroslav@67: * {@code long} representation to be parsed. jaroslav@67: * @param radix the radix to be used while parsing {@code s}. jaroslav@67: * @return the {@code long} represented by the string argument in jaroslav@67: * the specified radix. jaroslav@67: * @throws NumberFormatException if the string does not contain a jaroslav@67: * parsable {@code long}. jaroslav@67: */ jaroslav@67: public static long parseLong(String s, int radix) jaroslav@67: throws NumberFormatException jaroslav@67: { jaroslav@67: if (s == null) { jaroslav@67: throw new NumberFormatException("null"); jaroslav@67: } jaroslav@67: jaroslav@67: if (radix < Character.MIN_RADIX) { jaroslav@67: throw new NumberFormatException("radix " + radix + jaroslav@67: " less than Character.MIN_RADIX"); jaroslav@67: } jaroslav@67: if (radix > Character.MAX_RADIX) { jaroslav@67: throw new NumberFormatException("radix " + radix + jaroslav@67: " greater than Character.MAX_RADIX"); jaroslav@67: } jaroslav@67: jaroslav@67: long result = 0; jaroslav@67: boolean negative = false; jaroslav@67: int i = 0, len = s.length(); jaroslav@67: long limit = -Long.MAX_VALUE; jaroslav@67: long multmin; jaroslav@67: int digit; jaroslav@67: jaroslav@67: if (len > 0) { jaroslav@67: char firstChar = s.charAt(0); jaroslav@67: if (firstChar < '0') { // Possible leading "+" or "-" jaroslav@67: if (firstChar == '-') { jaroslav@67: negative = true; jaroslav@67: limit = Long.MIN_VALUE; jaroslav@67: } else if (firstChar != '+') jaroslav@67: throw NumberFormatException.forInputString(s); jaroslav@67: jaroslav@67: if (len == 1) // Cannot have lone "+" or "-" jaroslav@67: throw NumberFormatException.forInputString(s); jaroslav@67: i++; jaroslav@67: } jaroslav@67: multmin = limit / radix; jaroslav@67: while (i < len) { jaroslav@67: // Accumulating negatively avoids surprises near MAX_VALUE jaroslav@67: digit = Character.digit(s.charAt(i++),radix); jaroslav@67: if (digit < 0) { jaroslav@67: throw NumberFormatException.forInputString(s); jaroslav@67: } jaroslav@67: if (result < multmin) { jaroslav@67: throw NumberFormatException.forInputString(s); jaroslav@67: } jaroslav@67: result *= radix; jaroslav@67: if (result < limit + digit) { jaroslav@67: throw NumberFormatException.forInputString(s); jaroslav@67: } jaroslav@67: result -= digit; jaroslav@67: } jaroslav@67: } else { jaroslav@67: throw NumberFormatException.forInputString(s); jaroslav@67: } jaroslav@67: return negative ? result : -result; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Parses the string argument as a signed decimal {@code long}. jaroslav@67: * The characters in the string must all be decimal digits, except jaroslav@67: * that the first character may be an ASCII minus sign {@code '-'} jaroslav@67: * (\u002D') to indicate a negative value or an jaroslav@67: * ASCII plus sign {@code '+'} ('\u002B') to jaroslav@67: * indicate a positive value. The resulting {@code long} value is jaroslav@67: * returned, exactly as if the argument and the radix {@code 10} jaroslav@67: * were given as arguments to the {@link jaroslav@67: * #parseLong(java.lang.String, int)} method. jaroslav@67: * jaroslav@67: *

Note that neither the character {@code L} jaroslav@67: * ('\u004C') nor {@code l} jaroslav@67: * ('\u006C') is permitted to appear at the end jaroslav@67: * of the string as a type indicator, as would be permitted in jaroslav@67: * Java programming language source code. jaroslav@67: * jaroslav@67: * @param s a {@code String} containing the {@code long} jaroslav@67: * representation to be parsed jaroslav@67: * @return the {@code long} represented by the argument in jaroslav@67: * decimal. jaroslav@67: * @throws NumberFormatException if the string does not contain a jaroslav@67: * parsable {@code long}. jaroslav@67: */ jaroslav@67: public static long parseLong(String s) throws NumberFormatException { jaroslav@67: return parseLong(s, 10); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a {@code Long} object holding the value jaroslav@67: * extracted from the specified {@code String} when parsed jaroslav@67: * with the radix given by the second argument. The first jaroslav@67: * argument is interpreted as representing a signed jaroslav@67: * {@code long} in the radix specified by the second jaroslav@67: * argument, exactly as if the arguments were given to the {@link jaroslav@67: * #parseLong(java.lang.String, int)} method. The result is a jaroslav@67: * {@code Long} object that represents the {@code long} jaroslav@67: * value specified by the string. jaroslav@67: * jaroslav@67: *

In other words, this method returns a {@code Long} object equal jaroslav@67: * to the value of: jaroslav@67: * jaroslav@67: *

jaroslav@67: * {@code new Long(Long.parseLong(s, radix))} jaroslav@67: *
jaroslav@67: * jaroslav@67: * @param s the string to be parsed jaroslav@67: * @param radix the radix to be used in interpreting {@code s} jaroslav@67: * @return a {@code Long} object holding the value jaroslav@67: * represented by the string argument in the specified jaroslav@67: * radix. jaroslav@67: * @throws NumberFormatException If the {@code String} does not jaroslav@67: * contain a parsable {@code long}. jaroslav@67: */ jaroslav@67: public static Long valueOf(String s, int radix) throws NumberFormatException { jaroslav@67: return Long.valueOf(parseLong(s, radix)); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a {@code Long} object holding the value jaroslav@67: * of the specified {@code String}. The argument is jaroslav@67: * interpreted as representing a signed decimal {@code long}, jaroslav@67: * exactly as if the argument were given to the {@link jaroslav@67: * #parseLong(java.lang.String)} method. The result is a jaroslav@67: * {@code Long} object that represents the integer value jaroslav@67: * specified by the string. jaroslav@67: * jaroslav@67: *

In other words, this method returns a {@code Long} object jaroslav@67: * equal to the value of: jaroslav@67: * jaroslav@67: *

jaroslav@67: * {@code new Long(Long.parseLong(s))} jaroslav@67: *
jaroslav@67: * jaroslav@67: * @param s the string to be parsed. jaroslav@67: * @return a {@code Long} object holding the value jaroslav@67: * represented by the string argument. jaroslav@67: * @throws NumberFormatException If the string cannot be parsed jaroslav@67: * as a {@code long}. jaroslav@67: */ jaroslav@67: public static Long valueOf(String s) throws NumberFormatException jaroslav@67: { jaroslav@67: return Long.valueOf(parseLong(s, 10)); jaroslav@67: } jaroslav@67: jaroslav@67: private static class LongCache { jaroslav@67: private LongCache(){} jaroslav@67: jaroslav@67: static final Long cache[] = new Long[-(-128) + 127 + 1]; jaroslav@67: jaroslav@67: static { jaroslav@67: for(int i = 0; i < cache.length; i++) jaroslav@67: cache[i] = new Long(i - 128); jaroslav@67: } jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a {@code Long} instance representing the specified jaroslav@67: * {@code long} value. jaroslav@67: * If a new {@code Long} instance is not required, this method jaroslav@67: * should generally be used in preference to the constructor jaroslav@67: * {@link #Long(long)}, as this method is likely to yield jaroslav@67: * significantly better space and time performance by caching jaroslav@67: * frequently requested values. jaroslav@67: * jaroslav@67: * Note that unlike the {@linkplain Integer#valueOf(int) jaroslav@67: * corresponding method} in the {@code Integer} class, this method jaroslav@67: * is not required to cache values within a particular jaroslav@67: * range. jaroslav@67: * jaroslav@67: * @param l a long value. jaroslav@67: * @return a {@code Long} instance representing {@code l}. jaroslav@67: * @since 1.5 jaroslav@67: */ jaroslav@67: public static Long valueOf(long l) { jaroslav@67: final int offset = 128; jaroslav@67: if (l >= -128 && l <= 127) { // will cache jaroslav@67: return LongCache.cache[(int)l + offset]; jaroslav@67: } jaroslav@67: return new Long(l); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Decodes a {@code String} into a {@code Long}. jaroslav@67: * Accepts decimal, hexadecimal, and octal numbers given by the jaroslav@67: * following grammar: jaroslav@67: * jaroslav@67: *
jaroslav@67: *
jaroslav@67: *
DecodableString: jaroslav@67: *
Signopt DecimalNumeral jaroslav@67: *
Signopt {@code 0x} HexDigits jaroslav@67: *
Signopt {@code 0X} HexDigits jaroslav@67: *
Signopt {@code #} HexDigits jaroslav@67: *
Signopt {@code 0} OctalDigits jaroslav@67: *

jaroslav@67: *

Sign: jaroslav@67: *
{@code -} jaroslav@67: *
{@code +} jaroslav@67: *
jaroslav@67: *
jaroslav@67: * jaroslav@67: * DecimalNumeral, HexDigits, and OctalDigits jaroslav@67: * are as defined in section 3.10.1 of jaroslav@67: * The Java™ Language Specification, jaroslav@67: * except that underscores are not accepted between digits. jaroslav@67: * jaroslav@67: *

The sequence of characters following an optional jaroslav@67: * sign and/or radix specifier ("{@code 0x}", "{@code 0X}", jaroslav@67: * "{@code #}", or leading zero) is parsed as by the {@code jaroslav@67: * Long.parseLong} method with the indicated radix (10, 16, or 8). jaroslav@67: * This sequence of characters must represent a positive value or jaroslav@67: * a {@link NumberFormatException} will be thrown. The result is jaroslav@67: * negated if first character of the specified {@code String} is jaroslav@67: * the minus sign. No whitespace characters are permitted in the jaroslav@67: * {@code String}. jaroslav@67: * jaroslav@67: * @param nm the {@code String} to decode. jaroslav@67: * @return a {@code Long} object holding the {@code long} jaroslav@67: * value represented by {@code nm} jaroslav@67: * @throws NumberFormatException if the {@code String} does not jaroslav@67: * contain a parsable {@code long}. jaroslav@67: * @see java.lang.Long#parseLong(String, int) jaroslav@67: * @since 1.2 jaroslav@67: */ jaroslav@67: public static Long decode(String nm) throws NumberFormatException { jaroslav@67: int radix = 10; jaroslav@67: int index = 0; jaroslav@67: boolean negative = false; jaroslav@67: Long result; jaroslav@67: jaroslav@67: if (nm.length() == 0) jaroslav@67: throw new NumberFormatException("Zero length string"); jaroslav@67: char firstChar = nm.charAt(0); jaroslav@67: // Handle sign, if present jaroslav@67: if (firstChar == '-') { jaroslav@67: negative = true; jaroslav@67: index++; jaroslav@67: } else if (firstChar == '+') jaroslav@67: index++; jaroslav@67: jaroslav@67: // Handle radix specifier, if present jaroslav@67: if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) { jaroslav@67: index += 2; jaroslav@67: radix = 16; jaroslav@67: } jaroslav@67: else if (nm.startsWith("#", index)) { jaroslav@67: index ++; jaroslav@67: radix = 16; jaroslav@67: } jaroslav@67: else if (nm.startsWith("0", index) && nm.length() > 1 + index) { jaroslav@67: index ++; jaroslav@67: radix = 8; jaroslav@67: } jaroslav@67: jaroslav@67: if (nm.startsWith("-", index) || nm.startsWith("+", index)) jaroslav@67: throw new NumberFormatException("Sign character in wrong position"); jaroslav@67: jaroslav@67: try { jaroslav@67: result = Long.valueOf(nm.substring(index), radix); jaroslav@67: result = negative ? Long.valueOf(-result.longValue()) : result; jaroslav@67: } catch (NumberFormatException e) { jaroslav@67: // If number is Long.MIN_VALUE, we'll end up here. The next line jaroslav@67: // handles this case, and causes any genuine format error to be jaroslav@67: // rethrown. jaroslav@67: String constant = negative ? ("-" + nm.substring(index)) jaroslav@67: : nm.substring(index); jaroslav@67: result = Long.valueOf(constant, radix); jaroslav@67: } jaroslav@67: return result; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * The value of the {@code Long}. jaroslav@67: * jaroslav@67: * @serial jaroslav@67: */ jaroslav@67: private final long value; jaroslav@67: jaroslav@67: /** jaroslav@67: * Constructs a newly allocated {@code Long} object that jaroslav@67: * represents the specified {@code long} argument. jaroslav@67: * jaroslav@67: * @param value the value to be represented by the jaroslav@67: * {@code Long} object. jaroslav@67: */ jaroslav@67: public Long(long value) { jaroslav@67: this.value = value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Constructs a newly allocated {@code Long} object that jaroslav@67: * represents the {@code long} value indicated by the jaroslav@67: * {@code String} parameter. The string is converted to a jaroslav@67: * {@code long} value in exactly the manner used by the jaroslav@67: * {@code parseLong} method for radix 10. jaroslav@67: * jaroslav@67: * @param s the {@code String} to be converted to a jaroslav@67: * {@code Long}. jaroslav@67: * @throws NumberFormatException if the {@code String} does not jaroslav@67: * contain a parsable {@code long}. jaroslav@67: * @see java.lang.Long#parseLong(java.lang.String, int) jaroslav@67: */ jaroslav@67: public Long(String s) throws NumberFormatException { jaroslav@67: this.value = parseLong(s, 10); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the value of this {@code Long} as a jaroslav@67: * {@code byte}. jaroslav@67: */ jaroslav@67: public byte byteValue() { jaroslav@67: return (byte)value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the value of this {@code Long} as a jaroslav@67: * {@code short}. jaroslav@67: */ jaroslav@67: public short shortValue() { jaroslav@67: return (short)value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the value of this {@code Long} as an jaroslav@67: * {@code int}. jaroslav@67: */ jaroslav@67: public int intValue() { jaroslav@67: return (int)value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the value of this {@code Long} as a jaroslav@67: * {@code long} value. jaroslav@67: */ jaroslav@67: public long longValue() { jaroslav@67: return (long)value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the value of this {@code Long} as a jaroslav@67: * {@code float}. jaroslav@67: */ jaroslav@67: public float floatValue() { jaroslav@67: return (float)value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the value of this {@code Long} as a jaroslav@67: * {@code double}. jaroslav@67: */ jaroslav@67: public double doubleValue() { jaroslav@67: return (double)value; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a {@code String} object representing this jaroslav@67: * {@code Long}'s value. The value is converted to signed jaroslav@67: * decimal representation and returned as a string, exactly as if jaroslav@67: * the {@code long} value were given as an argument to the jaroslav@67: * {@link java.lang.Long#toString(long)} method. jaroslav@67: * jaroslav@67: * @return a string representation of the value of this object in jaroslav@67: * base 10. jaroslav@67: */ jaroslav@67: public String toString() { jaroslav@67: return toString(value); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a hash code for this {@code Long}. The result is jaroslav@67: * the exclusive OR of the two halves of the primitive jaroslav@67: * {@code long} value held by this {@code Long} jaroslav@67: * object. That is, the hashcode is the value of the expression: jaroslav@67: * jaroslav@67: *

jaroslav@67: * {@code (int)(this.longValue()^(this.longValue()>>>32))} jaroslav@67: *
jaroslav@67: * jaroslav@67: * @return a hash code value for this object. jaroslav@67: */ jaroslav@67: public int hashCode() { jaroslav@67: return (int)(value ^ (value >>> 32)); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Compares this object to the specified object. The result is jaroslav@67: * {@code true} if and only if the argument is not jaroslav@67: * {@code null} and is a {@code Long} object that jaroslav@67: * contains the same {@code long} value as this object. jaroslav@67: * jaroslav@67: * @param obj the object to compare with. jaroslav@67: * @return {@code true} if the objects are the same; jaroslav@67: * {@code false} otherwise. jaroslav@67: */ jaroslav@67: public boolean equals(Object obj) { jaroslav@67: if (obj instanceof Long) { jaroslav@67: return value == ((Long)obj).longValue(); jaroslav@67: } jaroslav@67: return false; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Determines the {@code long} value of the system property jaroslav@67: * with the specified name. jaroslav@67: * jaroslav@67: *

The first argument is treated as the name of a system property. jaroslav@67: * System properties are accessible through the {@link jaroslav@67: * java.lang.System#getProperty(java.lang.String)} method. The jaroslav@67: * string value of this property is then interpreted as a jaroslav@67: * {@code long} value and a {@code Long} object jaroslav@67: * representing this value is returned. Details of possible jaroslav@67: * numeric formats can be found with the definition of jaroslav@67: * {@code getProperty}. jaroslav@67: * jaroslav@67: *

If there is no property with the specified name, if the jaroslav@67: * specified name is empty or {@code null}, or if the jaroslav@67: * property does not have the correct numeric format, then jaroslav@67: * {@code null} is returned. jaroslav@67: * jaroslav@67: *

In other words, this method returns a {@code Long} object equal to jaroslav@67: * the value of: jaroslav@67: * jaroslav@67: *

jaroslav@67: * {@code getLong(nm, null)} jaroslav@67: *
jaroslav@67: * jaroslav@67: * @param nm property name. jaroslav@67: * @return the {@code Long} value of the property. jaroslav@67: * @see java.lang.System#getProperty(java.lang.String) jaroslav@67: * @see java.lang.System#getProperty(java.lang.String, java.lang.String) jaroslav@67: */ jaroslav@67: public static Long getLong(String nm) { jaroslav@67: return getLong(nm, null); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Determines the {@code long} value of the system property jaroslav@67: * with the specified name. jaroslav@67: * jaroslav@67: *

The first argument is treated as the name of a system property. jaroslav@67: * System properties are accessible through the {@link jaroslav@67: * java.lang.System#getProperty(java.lang.String)} method. The jaroslav@67: * string value of this property is then interpreted as a jaroslav@67: * {@code long} value and a {@code Long} object jaroslav@67: * representing this value is returned. Details of possible jaroslav@67: * numeric formats can be found with the definition of jaroslav@67: * {@code getProperty}. jaroslav@67: * jaroslav@67: *

The second argument is the default value. A {@code Long} object jaroslav@67: * that represents the value of the second argument is returned if there jaroslav@67: * is no property of the specified name, if the property does not have jaroslav@67: * the correct numeric format, or if the specified name is empty or null. jaroslav@67: * jaroslav@67: *

In other words, this method returns a {@code Long} object equal jaroslav@67: * to the value of: jaroslav@67: * jaroslav@67: *

jaroslav@67: * {@code getLong(nm, new Long(val))} jaroslav@67: *
jaroslav@67: * jaroslav@67: * but in practice it may be implemented in a manner such as: jaroslav@67: * jaroslav@67: *
jaroslav@67:      * Long result = getLong(nm, null);
jaroslav@67:      * return (result == null) ? new Long(val) : result;
jaroslav@67:      * 
jaroslav@67: * jaroslav@67: * to avoid the unnecessary allocation of a {@code Long} object when jaroslav@67: * the default value is not needed. jaroslav@67: * jaroslav@67: * @param nm property name. jaroslav@67: * @param val default value. jaroslav@67: * @return the {@code Long} value of the property. jaroslav@67: * @see java.lang.System#getProperty(java.lang.String) jaroslav@67: * @see java.lang.System#getProperty(java.lang.String, java.lang.String) jaroslav@67: */ jaroslav@67: public static Long getLong(String nm, long val) { jaroslav@67: Long result = Long.getLong(nm, null); jaroslav@67: return (result == null) ? Long.valueOf(val) : result; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the {@code long} value of the system property with jaroslav@67: * the specified name. The first argument is treated as the name jaroslav@67: * of a system property. System properties are accessible through jaroslav@67: * the {@link java.lang.System#getProperty(java.lang.String)} jaroslav@67: * method. The string value of this property is then interpreted jaroslav@67: * as a {@code long} value, as per the jaroslav@67: * {@code Long.decode} method, and a {@code Long} object jaroslav@67: * representing this value is returned. jaroslav@67: * jaroslav@67: * jaroslav@67: * jaroslav@67: *

Note that, in every case, neither {@code L} jaroslav@67: * ('\u004C') nor {@code l} jaroslav@67: * ('\u006C') is permitted to appear at the end jaroslav@67: * of the property value as a type indicator, as would be jaroslav@67: * permitted in Java programming language source code. jaroslav@67: * jaroslav@67: *

The second argument is the default value. The default value is jaroslav@67: * returned if there is no property of the specified name, if the jaroslav@67: * property does not have the correct numeric format, or if the jaroslav@67: * specified name is empty or {@code null}. jaroslav@67: * jaroslav@67: * @param nm property name. jaroslav@67: * @param val default value. jaroslav@67: * @return the {@code Long} value of the property. jaroslav@67: * @see java.lang.System#getProperty(java.lang.String) jaroslav@67: * @see java.lang.System#getProperty(java.lang.String, java.lang.String) jaroslav@67: * @see java.lang.Long#decode jaroslav@67: */ jaroslav@67: public static Long getLong(String nm, Long val) { jaroslav@67: String v = null; jaroslav@67: try { jaroslav@104: v = AbstractStringBuilder.getProperty(nm); jaroslav@67: } catch (IllegalArgumentException e) { jaroslav@67: } catch (NullPointerException e) { jaroslav@67: } jaroslav@67: if (v != null) { jaroslav@67: try { jaroslav@67: return Long.decode(v); jaroslav@67: } catch (NumberFormatException e) { jaroslav@67: } jaroslav@67: } jaroslav@67: return val; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Compares two {@code Long} objects numerically. jaroslav@67: * jaroslav@67: * @param anotherLong the {@code Long} to be compared. jaroslav@67: * @return the value {@code 0} if this {@code Long} is jaroslav@67: * equal to the argument {@code Long}; a value less than jaroslav@67: * {@code 0} if this {@code Long} is numerically less jaroslav@67: * than the argument {@code Long}; and a value greater jaroslav@67: * than {@code 0} if this {@code Long} is numerically jaroslav@67: * greater than the argument {@code Long} (signed jaroslav@67: * comparison). jaroslav@67: * @since 1.2 jaroslav@67: */ jaroslav@67: public int compareTo(Long anotherLong) { jaroslav@67: return compare(this.value, anotherLong.value); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Compares two {@code long} values numerically. jaroslav@67: * The value returned is identical to what would be returned by: jaroslav@67: *

jaroslav@67:      *    Long.valueOf(x).compareTo(Long.valueOf(y))
jaroslav@67:      * 
jaroslav@67: * jaroslav@67: * @param x the first {@code long} to compare jaroslav@67: * @param y the second {@code long} to compare jaroslav@67: * @return the value {@code 0} if {@code x == y}; jaroslav@67: * a value less than {@code 0} if {@code x < y}; and jaroslav@67: * a value greater than {@code 0} if {@code x > y} jaroslav@67: * @since 1.7 jaroslav@67: */ jaroslav@67: public static int compare(long x, long y) { jaroslav@67: return (x < y) ? -1 : ((x == y) ? 0 : 1); jaroslav@67: } jaroslav@67: jaroslav@67: jaroslav@67: // Bit Twiddling jaroslav@67: jaroslav@67: /** jaroslav@67: * The number of bits used to represent a {@code long} value in two's jaroslav@67: * complement binary form. jaroslav@67: * jaroslav@67: * @since 1.5 jaroslav@67: */ jaroslav@67: public static final int SIZE = 64; jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a {@code long} value with at most a single one-bit, in the jaroslav@67: * position of the highest-order ("leftmost") one-bit in the specified jaroslav@67: * {@code long} value. Returns zero if the specified value has no jaroslav@67: * one-bits in its two's complement binary representation, that is, if it jaroslav@67: * is equal to zero. jaroslav@67: * jaroslav@67: * @return a {@code long} value with a single one-bit, in the position jaroslav@67: * of the highest-order one-bit in the specified value, or zero if jaroslav@67: * the specified value is itself equal to zero. jaroslav@67: * @since 1.5 jaroslav@67: */ jaroslav@67: public static long highestOneBit(long i) { jaroslav@67: // HD, Figure 3-1 jaroslav@67: i |= (i >> 1); jaroslav@67: i |= (i >> 2); jaroslav@67: i |= (i >> 4); jaroslav@67: i |= (i >> 8); jaroslav@67: i |= (i >> 16); jaroslav@67: i |= (i >> 32); jaroslav@67: return i - (i >>> 1); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns a {@code long} value with at most a single one-bit, in the jaroslav@67: * position of the lowest-order ("rightmost") one-bit in the specified jaroslav@67: * {@code long} value. Returns zero if the specified value has no jaroslav@67: * one-bits in its two's complement binary representation, that is, if it jaroslav@67: * is equal to zero. jaroslav@67: * jaroslav@67: * @return a {@code long} value with a single one-bit, in the position jaroslav@67: * of the lowest-order one-bit in the specified value, or zero if jaroslav@67: * the specified value is itself equal to zero. jaroslav@67: * @since 1.5 jaroslav@67: */ jaroslav@67: public static long lowestOneBit(long i) { jaroslav@67: // HD, Section 2-1 jaroslav@67: return i & -i; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the number of zero bits preceding the highest-order jaroslav@67: * ("leftmost") one-bit in the two's complement binary representation jaroslav@67: * of the specified {@code long} value. Returns 64 if the jaroslav@67: * specified value has no one-bits in its two's complement representation, jaroslav@67: * in other words if it is equal to zero. jaroslav@67: * jaroslav@67: *

Note that this method is closely related to the logarithm base 2. jaroslav@67: * For all positive {@code long} values x: jaroslav@67: *

jaroslav@67: * jaroslav@67: * @return the number of zero bits preceding the highest-order jaroslav@67: * ("leftmost") one-bit in the two's complement binary representation jaroslav@67: * of the specified {@code long} value, or 64 if the value jaroslav@67: * is equal to zero. jaroslav@67: * @since 1.5 jaroslav@67: */ jaroslav@67: public static int numberOfLeadingZeros(long i) { jaroslav@67: // HD, Figure 5-6 jaroslav@67: if (i == 0) jaroslav@67: return 64; jaroslav@67: int n = 1; jaroslav@67: int x = (int)(i >>> 32); jaroslav@67: if (x == 0) { n += 32; x = (int)i; } jaroslav@67: if (x >>> 16 == 0) { n += 16; x <<= 16; } jaroslav@67: if (x >>> 24 == 0) { n += 8; x <<= 8; } jaroslav@67: if (x >>> 28 == 0) { n += 4; x <<= 4; } jaroslav@67: if (x >>> 30 == 0) { n += 2; x <<= 2; } jaroslav@67: n -= x >>> 31; jaroslav@67: return n; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the number of zero bits following the lowest-order ("rightmost") jaroslav@67: * one-bit in the two's complement binary representation of the specified jaroslav@67: * {@code long} value. Returns 64 if the specified value has no jaroslav@67: * one-bits in its two's complement representation, in other words if it is jaroslav@67: * equal to zero. jaroslav@67: * jaroslav@67: * @return the number of zero bits following the lowest-order ("rightmost") jaroslav@67: * one-bit in the two's complement binary representation of the jaroslav@67: * specified {@code long} value, or 64 if the value is equal jaroslav@67: * to zero. jaroslav@67: * @since 1.5 jaroslav@67: */ jaroslav@67: public static int numberOfTrailingZeros(long i) { jaroslav@67: // HD, Figure 5-14 jaroslav@67: int x, y; jaroslav@67: if (i == 0) return 64; jaroslav@67: int n = 63; jaroslav@67: y = (int)i; if (y != 0) { n = n -32; x = y; } else x = (int)(i>>>32); jaroslav@67: y = x <<16; if (y != 0) { n = n -16; x = y; } jaroslav@67: y = x << 8; if (y != 0) { n = n - 8; x = y; } jaroslav@67: y = x << 4; if (y != 0) { n = n - 4; x = y; } jaroslav@67: y = x << 2; if (y != 0) { n = n - 2; x = y; } jaroslav@67: return n - ((x << 1) >>> 31); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the number of one-bits in the two's complement binary jaroslav@67: * representation of the specified {@code long} value. This function is jaroslav@67: * sometimes referred to as the population count. jaroslav@67: * jaroslav@67: * @return the number of one-bits in the two's complement binary jaroslav@67: * representation of the specified {@code long} value. jaroslav@67: * @since 1.5 jaroslav@67: */ jaroslav@67: public static int bitCount(long i) { jaroslav@67: // HD, Figure 5-14 jaroslav@67: i = i - ((i >>> 1) & 0x5555555555555555L); jaroslav@67: i = (i & 0x3333333333333333L) + ((i >>> 2) & 0x3333333333333333L); jaroslav@67: i = (i + (i >>> 4)) & 0x0f0f0f0f0f0f0f0fL; jaroslav@67: i = i + (i >>> 8); jaroslav@67: i = i + (i >>> 16); jaroslav@67: i = i + (i >>> 32); jaroslav@67: return (int)i & 0x7f; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the value obtained by rotating the two's complement binary jaroslav@67: * representation of the specified {@code long} value left by the jaroslav@67: * specified number of bits. (Bits shifted out of the left hand, or jaroslav@67: * high-order, side reenter on the right, or low-order.) jaroslav@67: * jaroslav@67: *

Note that left rotation with a negative distance is equivalent to jaroslav@67: * right rotation: {@code rotateLeft(val, -distance) == rotateRight(val, jaroslav@67: * distance)}. Note also that rotation by any multiple of 64 is a jaroslav@67: * no-op, so all but the last six bits of the rotation distance can be jaroslav@67: * ignored, even if the distance is negative: {@code rotateLeft(val, jaroslav@67: * distance) == rotateLeft(val, distance & 0x3F)}. jaroslav@67: * jaroslav@67: * @return the value obtained by rotating the two's complement binary jaroslav@67: * representation of the specified {@code long} value left by the jaroslav@67: * specified number of bits. jaroslav@67: * @since 1.5 jaroslav@67: */ jaroslav@67: public static long rotateLeft(long i, int distance) { jaroslav@67: return (i << distance) | (i >>> -distance); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the value obtained by rotating the two's complement binary jaroslav@67: * representation of the specified {@code long} value right by the jaroslav@67: * specified number of bits. (Bits shifted out of the right hand, or jaroslav@67: * low-order, side reenter on the left, or high-order.) jaroslav@67: * jaroslav@67: *

Note that right rotation with a negative distance is equivalent to jaroslav@67: * left rotation: {@code rotateRight(val, -distance) == rotateLeft(val, jaroslav@67: * distance)}. Note also that rotation by any multiple of 64 is a jaroslav@67: * no-op, so all but the last six bits of the rotation distance can be jaroslav@67: * ignored, even if the distance is negative: {@code rotateRight(val, jaroslav@67: * distance) == rotateRight(val, distance & 0x3F)}. jaroslav@67: * jaroslav@67: * @return the value obtained by rotating the two's complement binary jaroslav@67: * representation of the specified {@code long} value right by the jaroslav@67: * specified number of bits. jaroslav@67: * @since 1.5 jaroslav@67: */ jaroslav@67: public static long rotateRight(long i, int distance) { jaroslav@67: return (i >>> distance) | (i << -distance); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the value obtained by reversing the order of the bits in the jaroslav@67: * two's complement binary representation of the specified {@code long} jaroslav@67: * value. jaroslav@67: * jaroslav@67: * @return the value obtained by reversing order of the bits in the jaroslav@67: * specified {@code long} value. jaroslav@67: * @since 1.5 jaroslav@67: */ jaroslav@67: public static long reverse(long i) { jaroslav@67: // HD, Figure 7-1 jaroslav@67: i = (i & 0x5555555555555555L) << 1 | (i >>> 1) & 0x5555555555555555L; jaroslav@67: i = (i & 0x3333333333333333L) << 2 | (i >>> 2) & 0x3333333333333333L; jaroslav@67: i = (i & 0x0f0f0f0f0f0f0f0fL) << 4 | (i >>> 4) & 0x0f0f0f0f0f0f0f0fL; jaroslav@67: i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL; jaroslav@67: i = (i << 48) | ((i & 0xffff0000L) << 16) | jaroslav@67: ((i >>> 16) & 0xffff0000L) | (i >>> 48); jaroslav@67: return i; jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the signum function of the specified {@code long} value. (The jaroslav@67: * return value is -1 if the specified value is negative; 0 if the jaroslav@67: * specified value is zero; and 1 if the specified value is positive.) jaroslav@67: * jaroslav@67: * @return the signum function of the specified {@code long} value. jaroslav@67: * @since 1.5 jaroslav@67: */ jaroslav@67: public static int signum(long i) { jaroslav@67: // HD, Section 2-7 jaroslav@67: return (int) ((i >> 63) | (-i >>> 63)); jaroslav@67: } jaroslav@67: jaroslav@67: /** jaroslav@67: * Returns the value obtained by reversing the order of the bytes in the jaroslav@67: * two's complement representation of the specified {@code long} value. jaroslav@67: * jaroslav@67: * @return the value obtained by reversing the bytes in the specified jaroslav@67: * {@code long} value. jaroslav@67: * @since 1.5 jaroslav@67: */ jaroslav@67: public static long reverseBytes(long i) { jaroslav@67: i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL; jaroslav@67: return (i << 48) | ((i & 0xffff0000L) << 16) | jaroslav@67: ((i >>> 16) & 0xffff0000L) | (i >>> 48); jaroslav@67: } jaroslav@67: jaroslav@67: /** use serialVersionUID from JDK 1.0.2 for interoperability */ jaroslav@67: private static final long serialVersionUID = 4290774380558885855L; jaroslav@67: }