1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/mini/src/main/java/java/lang/Long.java Wed Jan 23 20:39:23 2013 +0100
1.3 @@ -0,0 +1,1202 @@
1.4 +/*
1.5 + * Copyright (c) 1994, 2009, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.lang;
1.30 +
1.31 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.32 +
1.33 +/**
1.34 + * The {@code Long} class wraps a value of the primitive type {@code
1.35 + * long} in an object. An object of type {@code Long} contains a
1.36 + * single field whose type is {@code long}.
1.37 + *
1.38 + * <p> In addition, this class provides several methods for converting
1.39 + * a {@code long} to a {@code String} and a {@code String} to a {@code
1.40 + * long}, as well as other constants and methods useful when dealing
1.41 + * with a {@code long}.
1.42 + *
1.43 + * <p>Implementation note: The implementations of the "bit twiddling"
1.44 + * methods (such as {@link #highestOneBit(long) highestOneBit} and
1.45 + * {@link #numberOfTrailingZeros(long) numberOfTrailingZeros}) are
1.46 + * based on material from Henry S. Warren, Jr.'s <i>Hacker's
1.47 + * Delight</i>, (Addison Wesley, 2002).
1.48 + *
1.49 + * @author Lee Boynton
1.50 + * @author Arthur van Hoff
1.51 + * @author Josh Bloch
1.52 + * @author Joseph D. Darcy
1.53 + * @since JDK1.0
1.54 + */
1.55 +public final class Long extends Number implements Comparable<Long> {
1.56 + /**
1.57 + * A constant holding the minimum value a {@code long} can
1.58 + * have, -2<sup>63</sup>.
1.59 + */
1.60 + public static final long MIN_VALUE = 0x8000000000000000L;
1.61 +
1.62 + /**
1.63 + * A constant holding the maximum value a {@code long} can
1.64 + * have, 2<sup>63</sup>-1.
1.65 + */
1.66 + public static final long MAX_VALUE = 0x7fffffffffffffffL;
1.67 +
1.68 + /**
1.69 + * The {@code Class} instance representing the primitive type
1.70 + * {@code long}.
1.71 + *
1.72 + * @since JDK1.1
1.73 + */
1.74 + public static final Class<Long> TYPE = (Class<Long>) Class.getPrimitiveClass("long");
1.75 +
1.76 + /**
1.77 + * Returns a string representation of the first argument in the
1.78 + * radix specified by the second argument.
1.79 + *
1.80 + * <p>If the radix is smaller than {@code Character.MIN_RADIX}
1.81 + * or larger than {@code Character.MAX_RADIX}, then the radix
1.82 + * {@code 10} is used instead.
1.83 + *
1.84 + * <p>If the first argument is negative, the first element of the
1.85 + * result is the ASCII minus sign {@code '-'}
1.86 + * (<code>'\u002d'</code>). If the first argument is not
1.87 + * negative, no sign character appears in the result.
1.88 + *
1.89 + * <p>The remaining characters of the result represent the magnitude
1.90 + * of the first argument. If the magnitude is zero, it is
1.91 + * represented by a single zero character {@code '0'}
1.92 + * (<code>'\u0030'</code>); otherwise, the first character of
1.93 + * the representation of the magnitude will not be the zero
1.94 + * character. The following ASCII characters are used as digits:
1.95 + *
1.96 + * <blockquote>
1.97 + * {@code 0123456789abcdefghijklmnopqrstuvwxyz}
1.98 + * </blockquote>
1.99 + *
1.100 + * These are <code>'\u0030'</code> through
1.101 + * <code>'\u0039'</code> and <code>'\u0061'</code> through
1.102 + * <code>'\u007a'</code>. If {@code radix} is
1.103 + * <var>N</var>, then the first <var>N</var> of these characters
1.104 + * are used as radix-<var>N</var> digits in the order shown. Thus,
1.105 + * the digits for hexadecimal (radix 16) are
1.106 + * {@code 0123456789abcdef}. If uppercase letters are
1.107 + * desired, the {@link java.lang.String#toUpperCase()} method may
1.108 + * be called on the result:
1.109 + *
1.110 + * <blockquote>
1.111 + * {@code Long.toString(n, 16).toUpperCase()}
1.112 + * </blockquote>
1.113 + *
1.114 + * @param i a {@code long} to be converted to a string.
1.115 + * @param radix the radix to use in the string representation.
1.116 + * @return a string representation of the argument in the specified radix.
1.117 + * @see java.lang.Character#MAX_RADIX
1.118 + * @see java.lang.Character#MIN_RADIX
1.119 + */
1.120 + public static String toString(long i, int radix) {
1.121 + if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
1.122 + radix = 10;
1.123 + if (radix == 10)
1.124 + return toString(i);
1.125 + char[] buf = new char[65];
1.126 + int charPos = 64;
1.127 + boolean negative = (i < 0);
1.128 +
1.129 + if (!negative) {
1.130 + i = -i;
1.131 + }
1.132 +
1.133 + while (i <= -radix) {
1.134 + buf[charPos--] = Integer.digits[(int)(-(i % radix))];
1.135 + i = i / radix;
1.136 + }
1.137 + buf[charPos] = Integer.digits[(int)(-i)];
1.138 +
1.139 + if (negative) {
1.140 + buf[--charPos] = '-';
1.141 + }
1.142 +
1.143 + return new String(buf, charPos, (65 - charPos));
1.144 + }
1.145 +
1.146 + /**
1.147 + * Returns a string representation of the {@code long}
1.148 + * argument as an unsigned integer in base 16.
1.149 + *
1.150 + * <p>The unsigned {@code long} value is the argument plus
1.151 + * 2<sup>64</sup> if the argument is negative; otherwise, it is
1.152 + * equal to the argument. This value is converted to a string of
1.153 + * ASCII digits in hexadecimal (base 16) with no extra
1.154 + * leading {@code 0}s. If the unsigned magnitude is zero, it
1.155 + * is represented by a single zero character {@code '0'}
1.156 + * (<code>'\u0030'</code>); otherwise, the first character of
1.157 + * the representation of the unsigned magnitude will not be the
1.158 + * zero character. The following characters are used as
1.159 + * hexadecimal digits:
1.160 + *
1.161 + * <blockquote>
1.162 + * {@code 0123456789abcdef}
1.163 + * </blockquote>
1.164 + *
1.165 + * These are the characters <code>'\u0030'</code> through
1.166 + * <code>'\u0039'</code> and <code>'\u0061'</code> through
1.167 + * <code>'\u0066'</code>. If uppercase letters are desired,
1.168 + * the {@link java.lang.String#toUpperCase()} method may be called
1.169 + * on the result:
1.170 + *
1.171 + * <blockquote>
1.172 + * {@code Long.toHexString(n).toUpperCase()}
1.173 + * </blockquote>
1.174 + *
1.175 + * @param i a {@code long} to be converted to a string.
1.176 + * @return the string representation of the unsigned {@code long}
1.177 + * value represented by the argument in hexadecimal
1.178 + * (base 16).
1.179 + * @since JDK 1.0.2
1.180 + */
1.181 + public static String toHexString(long i) {
1.182 + return toUnsignedString(i, 4);
1.183 + }
1.184 +
1.185 + /**
1.186 + * Returns a string representation of the {@code long}
1.187 + * argument as an unsigned integer in base 8.
1.188 + *
1.189 + * <p>The unsigned {@code long} value is the argument plus
1.190 + * 2<sup>64</sup> if the argument is negative; otherwise, it is
1.191 + * equal to the argument. This value is converted to a string of
1.192 + * ASCII digits in octal (base 8) with no extra leading
1.193 + * {@code 0}s.
1.194 + *
1.195 + * <p>If the unsigned magnitude is zero, it is represented by a
1.196 + * single zero character {@code '0'}
1.197 + * (<code>'\u0030'</code>); otherwise, the first character of
1.198 + * the representation of the unsigned magnitude will not be the
1.199 + * zero character. The following characters are used as octal
1.200 + * digits:
1.201 + *
1.202 + * <blockquote>
1.203 + * {@code 01234567}
1.204 + * </blockquote>
1.205 + *
1.206 + * These are the characters <code>'\u0030'</code> through
1.207 + * <code>'\u0037'</code>.
1.208 + *
1.209 + * @param i a {@code long} to be converted to a string.
1.210 + * @return the string representation of the unsigned {@code long}
1.211 + * value represented by the argument in octal (base 8).
1.212 + * @since JDK 1.0.2
1.213 + */
1.214 + public static String toOctalString(long i) {
1.215 + return toUnsignedString(i, 3);
1.216 + }
1.217 +
1.218 + /**
1.219 + * Returns a string representation of the {@code long}
1.220 + * argument as an unsigned integer in base 2.
1.221 + *
1.222 + * <p>The unsigned {@code long} value is the argument plus
1.223 + * 2<sup>64</sup> if the argument is negative; otherwise, it is
1.224 + * equal to the argument. This value is converted to a string of
1.225 + * ASCII digits in binary (base 2) with no extra leading
1.226 + * {@code 0}s. If the unsigned magnitude is zero, it is
1.227 + * represented by a single zero character {@code '0'}
1.228 + * (<code>'\u0030'</code>); otherwise, the first character of
1.229 + * the representation of the unsigned magnitude will not be the
1.230 + * zero character. The characters {@code '0'}
1.231 + * (<code>'\u0030'</code>) and {@code '1'}
1.232 + * (<code>'\u0031'</code>) are used as binary digits.
1.233 + *
1.234 + * @param i a {@code long} to be converted to a string.
1.235 + * @return the string representation of the unsigned {@code long}
1.236 + * value represented by the argument in binary (base 2).
1.237 + * @since JDK 1.0.2
1.238 + */
1.239 + public static String toBinaryString(long i) {
1.240 + return toUnsignedString(i, 1);
1.241 + }
1.242 +
1.243 + /**
1.244 + * Convert the integer to an unsigned number.
1.245 + */
1.246 + private static String toUnsignedString(long i, int shift) {
1.247 + char[] buf = new char[64];
1.248 + int charPos = 64;
1.249 + int radix = 1 << shift;
1.250 + long mask = radix - 1;
1.251 + do {
1.252 + buf[--charPos] = Integer.digits[(int)(i & mask)];
1.253 + i >>>= shift;
1.254 + } while (i != 0);
1.255 + return new String(buf, charPos, (64 - charPos));
1.256 + }
1.257 +
1.258 + /**
1.259 + * Returns a {@code String} object representing the specified
1.260 + * {@code long}. The argument is converted to signed decimal
1.261 + * representation and returned as a string, exactly as if the
1.262 + * argument and the radix 10 were given as arguments to the {@link
1.263 + * #toString(long, int)} method.
1.264 + *
1.265 + * @param i a {@code long} to be converted.
1.266 + * @return a string representation of the argument in base 10.
1.267 + */
1.268 + @JavaScriptBody(args = "i", body = "return i.toString();")
1.269 + public static String toString(long i) {
1.270 + if (i == Long.MIN_VALUE)
1.271 + return "-9223372036854775808";
1.272 + int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
1.273 + char[] buf = new char[size];
1.274 + getChars(i, size, buf);
1.275 + return new String(buf, 0, size);
1.276 + }
1.277 +
1.278 + /**
1.279 + * Places characters representing the integer i into the
1.280 + * character array buf. The characters are placed into
1.281 + * the buffer backwards starting with the least significant
1.282 + * digit at the specified index (exclusive), and working
1.283 + * backwards from there.
1.284 + *
1.285 + * Will fail if i == Long.MIN_VALUE
1.286 + */
1.287 + static void getChars(long i, int index, char[] buf) {
1.288 + long q;
1.289 + int r;
1.290 + int charPos = index;
1.291 + char sign = 0;
1.292 +
1.293 + if (i < 0) {
1.294 + sign = '-';
1.295 + i = -i;
1.296 + }
1.297 +
1.298 + // Get 2 digits/iteration using longs until quotient fits into an int
1.299 + while (i > Integer.MAX_VALUE) {
1.300 + q = i / 100;
1.301 + // really: r = i - (q * 100);
1.302 + r = (int)(i - ((q << 6) + (q << 5) + (q << 2)));
1.303 + i = q;
1.304 + buf[--charPos] = Integer.DigitOnes[r];
1.305 + buf[--charPos] = Integer.DigitTens[r];
1.306 + }
1.307 +
1.308 + // Get 2 digits/iteration using ints
1.309 + int q2;
1.310 + int i2 = (int)i;
1.311 + while (i2 >= 65536) {
1.312 + q2 = i2 / 100;
1.313 + // really: r = i2 - (q * 100);
1.314 + r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2));
1.315 + i2 = q2;
1.316 + buf[--charPos] = Integer.DigitOnes[r];
1.317 + buf[--charPos] = Integer.DigitTens[r];
1.318 + }
1.319 +
1.320 + // Fall thru to fast mode for smaller numbers
1.321 + // assert(i2 <= 65536, i2);
1.322 + for (;;) {
1.323 + q2 = (i2 * 52429) >>> (16+3);
1.324 + r = i2 - ((q2 << 3) + (q2 << 1)); // r = i2-(q2*10) ...
1.325 + buf[--charPos] = Integer.digits[r];
1.326 + i2 = q2;
1.327 + if (i2 == 0) break;
1.328 + }
1.329 + if (sign != 0) {
1.330 + buf[--charPos] = sign;
1.331 + }
1.332 + }
1.333 +
1.334 + // Requires positive x
1.335 + static int stringSize(long x) {
1.336 + long p = 10;
1.337 + for (int i=1; i<19; i++) {
1.338 + if (x < p)
1.339 + return i;
1.340 + p = 10*p;
1.341 + }
1.342 + return 19;
1.343 + }
1.344 +
1.345 + /**
1.346 + * Parses the string argument as a signed {@code long} in the
1.347 + * radix specified by the second argument. The characters in the
1.348 + * string must all be digits of the specified radix (as determined
1.349 + * by whether {@link java.lang.Character#digit(char, int)} returns
1.350 + * a nonnegative value), except that the first character may be an
1.351 + * ASCII minus sign {@code '-'} (<code>'\u002D'</code>) to
1.352 + * indicate a negative value or an ASCII plus sign {@code '+'}
1.353 + * (<code>'\u002B'</code>) to indicate a positive value. The
1.354 + * resulting {@code long} value is returned.
1.355 + *
1.356 + * <p>Note that neither the character {@code L}
1.357 + * (<code>'\u004C'</code>) nor {@code l}
1.358 + * (<code>'\u006C'</code>) is permitted to appear at the end
1.359 + * of the string as a type indicator, as would be permitted in
1.360 + * Java programming language source code - except that either
1.361 + * {@code L} or {@code l} may appear as a digit for a
1.362 + * radix greater than 22.
1.363 + *
1.364 + * <p>An exception of type {@code NumberFormatException} is
1.365 + * thrown if any of the following situations occurs:
1.366 + * <ul>
1.367 + *
1.368 + * <li>The first argument is {@code null} or is a string of
1.369 + * length zero.
1.370 + *
1.371 + * <li>The {@code radix} is either smaller than {@link
1.372 + * java.lang.Character#MIN_RADIX} or larger than {@link
1.373 + * java.lang.Character#MAX_RADIX}.
1.374 + *
1.375 + * <li>Any character of the string is not a digit of the specified
1.376 + * radix, except that the first character may be a minus sign
1.377 + * {@code '-'} (<code>'\u002d'</code>) or plus sign {@code
1.378 + * '+'} (<code>'\u002B'</code>) provided that the string is
1.379 + * longer than length 1.
1.380 + *
1.381 + * <li>The value represented by the string is not a value of type
1.382 + * {@code long}.
1.383 + * </ul>
1.384 + *
1.385 + * <p>Examples:
1.386 + * <blockquote><pre>
1.387 + * parseLong("0", 10) returns 0L
1.388 + * parseLong("473", 10) returns 473L
1.389 + * parseLong("+42", 10) returns 42L
1.390 + * parseLong("-0", 10) returns 0L
1.391 + * parseLong("-FF", 16) returns -255L
1.392 + * parseLong("1100110", 2) returns 102L
1.393 + * parseLong("99", 8) throws a NumberFormatException
1.394 + * parseLong("Hazelnut", 10) throws a NumberFormatException
1.395 + * parseLong("Hazelnut", 36) returns 1356099454469L
1.396 + * </pre></blockquote>
1.397 + *
1.398 + * @param s the {@code String} containing the
1.399 + * {@code long} representation to be parsed.
1.400 + * @param radix the radix to be used while parsing {@code s}.
1.401 + * @return the {@code long} represented by the string argument in
1.402 + * the specified radix.
1.403 + * @throws NumberFormatException if the string does not contain a
1.404 + * parsable {@code long}.
1.405 + */
1.406 + public static long parseLong(String s, int radix)
1.407 + throws NumberFormatException
1.408 + {
1.409 + if (s == null) {
1.410 + throw new NumberFormatException("null");
1.411 + }
1.412 +
1.413 + if (radix < Character.MIN_RADIX) {
1.414 + throw new NumberFormatException("radix " + radix +
1.415 + " less than Character.MIN_RADIX");
1.416 + }
1.417 + if (radix > Character.MAX_RADIX) {
1.418 + throw new NumberFormatException("radix " + radix +
1.419 + " greater than Character.MAX_RADIX");
1.420 + }
1.421 +
1.422 + long result = 0;
1.423 + boolean negative = false;
1.424 + int i = 0, len = s.length();
1.425 + long limit = -Long.MAX_VALUE;
1.426 + long multmin;
1.427 + int digit;
1.428 +
1.429 + if (len > 0) {
1.430 + char firstChar = s.charAt(0);
1.431 + if (firstChar < '0') { // Possible leading "+" or "-"
1.432 + if (firstChar == '-') {
1.433 + negative = true;
1.434 + limit = Long.MIN_VALUE;
1.435 + } else if (firstChar != '+')
1.436 + throw NumberFormatException.forInputString(s);
1.437 +
1.438 + if (len == 1) // Cannot have lone "+" or "-"
1.439 + throw NumberFormatException.forInputString(s);
1.440 + i++;
1.441 + }
1.442 + multmin = limit / radix;
1.443 + while (i < len) {
1.444 + // Accumulating negatively avoids surprises near MAX_VALUE
1.445 + digit = Character.digit(s.charAt(i++),radix);
1.446 + if (digit < 0) {
1.447 + throw NumberFormatException.forInputString(s);
1.448 + }
1.449 + if (result < multmin) {
1.450 + throw NumberFormatException.forInputString(s);
1.451 + }
1.452 + result *= radix;
1.453 + if (result < limit + digit) {
1.454 + throw NumberFormatException.forInputString(s);
1.455 + }
1.456 + result -= digit;
1.457 + }
1.458 + } else {
1.459 + throw NumberFormatException.forInputString(s);
1.460 + }
1.461 + return negative ? result : -result;
1.462 + }
1.463 +
1.464 + /**
1.465 + * Parses the string argument as a signed decimal {@code long}.
1.466 + * The characters in the string must all be decimal digits, except
1.467 + * that the first character may be an ASCII minus sign {@code '-'}
1.468 + * (<code>\u002D'</code>) to indicate a negative value or an
1.469 + * ASCII plus sign {@code '+'} (<code>'\u002B'</code>) to
1.470 + * indicate a positive value. The resulting {@code long} value is
1.471 + * returned, exactly as if the argument and the radix {@code 10}
1.472 + * were given as arguments to the {@link
1.473 + * #parseLong(java.lang.String, int)} method.
1.474 + *
1.475 + * <p>Note that neither the character {@code L}
1.476 + * (<code>'\u004C'</code>) nor {@code l}
1.477 + * (<code>'\u006C'</code>) is permitted to appear at the end
1.478 + * of the string as a type indicator, as would be permitted in
1.479 + * Java programming language source code.
1.480 + *
1.481 + * @param s a {@code String} containing the {@code long}
1.482 + * representation to be parsed
1.483 + * @return the {@code long} represented by the argument in
1.484 + * decimal.
1.485 + * @throws NumberFormatException if the string does not contain a
1.486 + * parsable {@code long}.
1.487 + */
1.488 + public static long parseLong(String s) throws NumberFormatException {
1.489 + return parseLong(s, 10);
1.490 + }
1.491 +
1.492 + /**
1.493 + * Returns a {@code Long} object holding the value
1.494 + * extracted from the specified {@code String} when parsed
1.495 + * with the radix given by the second argument. The first
1.496 + * argument is interpreted as representing a signed
1.497 + * {@code long} in the radix specified by the second
1.498 + * argument, exactly as if the arguments were given to the {@link
1.499 + * #parseLong(java.lang.String, int)} method. The result is a
1.500 + * {@code Long} object that represents the {@code long}
1.501 + * value specified by the string.
1.502 + *
1.503 + * <p>In other words, this method returns a {@code Long} object equal
1.504 + * to the value of:
1.505 + *
1.506 + * <blockquote>
1.507 + * {@code new Long(Long.parseLong(s, radix))}
1.508 + * </blockquote>
1.509 + *
1.510 + * @param s the string to be parsed
1.511 + * @param radix the radix to be used in interpreting {@code s}
1.512 + * @return a {@code Long} object holding the value
1.513 + * represented by the string argument in the specified
1.514 + * radix.
1.515 + * @throws NumberFormatException If the {@code String} does not
1.516 + * contain a parsable {@code long}.
1.517 + */
1.518 + public static Long valueOf(String s, int radix) throws NumberFormatException {
1.519 + return Long.valueOf(parseLong(s, radix));
1.520 + }
1.521 +
1.522 + /**
1.523 + * Returns a {@code Long} object holding the value
1.524 + * of the specified {@code String}. The argument is
1.525 + * interpreted as representing a signed decimal {@code long},
1.526 + * exactly as if the argument were given to the {@link
1.527 + * #parseLong(java.lang.String)} method. The result is a
1.528 + * {@code Long} object that represents the integer value
1.529 + * specified by the string.
1.530 + *
1.531 + * <p>In other words, this method returns a {@code Long} object
1.532 + * equal to the value of:
1.533 + *
1.534 + * <blockquote>
1.535 + * {@code new Long(Long.parseLong(s))}
1.536 + * </blockquote>
1.537 + *
1.538 + * @param s the string to be parsed.
1.539 + * @return a {@code Long} object holding the value
1.540 + * represented by the string argument.
1.541 + * @throws NumberFormatException If the string cannot be parsed
1.542 + * as a {@code long}.
1.543 + */
1.544 + public static Long valueOf(String s) throws NumberFormatException
1.545 + {
1.546 + return Long.valueOf(parseLong(s, 10));
1.547 + }
1.548 +
1.549 + private static class LongCache {
1.550 + private LongCache(){}
1.551 +
1.552 + static final Long cache[] = new Long[-(-128) + 127 + 1];
1.553 +
1.554 + static {
1.555 + for(int i = 0; i < cache.length; i++)
1.556 + cache[i] = new Long(i - 128);
1.557 + }
1.558 + }
1.559 +
1.560 + /**
1.561 + * Returns a {@code Long} instance representing the specified
1.562 + * {@code long} value.
1.563 + * If a new {@code Long} instance is not required, this method
1.564 + * should generally be used in preference to the constructor
1.565 + * {@link #Long(long)}, as this method is likely to yield
1.566 + * significantly better space and time performance by caching
1.567 + * frequently requested values.
1.568 + *
1.569 + * Note that unlike the {@linkplain Integer#valueOf(int)
1.570 + * corresponding method} in the {@code Integer} class, this method
1.571 + * is <em>not</em> required to cache values within a particular
1.572 + * range.
1.573 + *
1.574 + * @param l a long value.
1.575 + * @return a {@code Long} instance representing {@code l}.
1.576 + * @since 1.5
1.577 + */
1.578 + public static Long valueOf(long l) {
1.579 + final int offset = 128;
1.580 + if (l >= -128 && l <= 127) { // will cache
1.581 + return LongCache.cache[(int)l + offset];
1.582 + }
1.583 + return new Long(l);
1.584 + }
1.585 +
1.586 + /**
1.587 + * Decodes a {@code String} into a {@code Long}.
1.588 + * Accepts decimal, hexadecimal, and octal numbers given by the
1.589 + * following grammar:
1.590 + *
1.591 + * <blockquote>
1.592 + * <dl>
1.593 + * <dt><i>DecodableString:</i>
1.594 + * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
1.595 + * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
1.596 + * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
1.597 + * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
1.598 + * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
1.599 + * <p>
1.600 + * <dt><i>Sign:</i>
1.601 + * <dd>{@code -}
1.602 + * <dd>{@code +}
1.603 + * </dl>
1.604 + * </blockquote>
1.605 + *
1.606 + * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
1.607 + * are as defined in section 3.10.1 of
1.608 + * <cite>The Java™ Language Specification</cite>,
1.609 + * except that underscores are not accepted between digits.
1.610 + *
1.611 + * <p>The sequence of characters following an optional
1.612 + * sign and/or radix specifier ("{@code 0x}", "{@code 0X}",
1.613 + * "{@code #}", or leading zero) is parsed as by the {@code
1.614 + * Long.parseLong} method with the indicated radix (10, 16, or 8).
1.615 + * This sequence of characters must represent a positive value or
1.616 + * a {@link NumberFormatException} will be thrown. The result is
1.617 + * negated if first character of the specified {@code String} is
1.618 + * the minus sign. No whitespace characters are permitted in the
1.619 + * {@code String}.
1.620 + *
1.621 + * @param nm the {@code String} to decode.
1.622 + * @return a {@code Long} object holding the {@code long}
1.623 + * value represented by {@code nm}
1.624 + * @throws NumberFormatException if the {@code String} does not
1.625 + * contain a parsable {@code long}.
1.626 + * @see java.lang.Long#parseLong(String, int)
1.627 + * @since 1.2
1.628 + */
1.629 + public static Long decode(String nm) throws NumberFormatException {
1.630 + int radix = 10;
1.631 + int index = 0;
1.632 + boolean negative = false;
1.633 + Long result;
1.634 +
1.635 + if (nm.length() == 0)
1.636 + throw new NumberFormatException("Zero length string");
1.637 + char firstChar = nm.charAt(0);
1.638 + // Handle sign, if present
1.639 + if (firstChar == '-') {
1.640 + negative = true;
1.641 + index++;
1.642 + } else if (firstChar == '+')
1.643 + index++;
1.644 +
1.645 + // Handle radix specifier, if present
1.646 + if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
1.647 + index += 2;
1.648 + radix = 16;
1.649 + }
1.650 + else if (nm.startsWith("#", index)) {
1.651 + index ++;
1.652 + radix = 16;
1.653 + }
1.654 + else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
1.655 + index ++;
1.656 + radix = 8;
1.657 + }
1.658 +
1.659 + if (nm.startsWith("-", index) || nm.startsWith("+", index))
1.660 + throw new NumberFormatException("Sign character in wrong position");
1.661 +
1.662 + try {
1.663 + result = Long.valueOf(nm.substring(index), radix);
1.664 + result = negative ? Long.valueOf(-result.longValue()) : result;
1.665 + } catch (NumberFormatException e) {
1.666 + // If number is Long.MIN_VALUE, we'll end up here. The next line
1.667 + // handles this case, and causes any genuine format error to be
1.668 + // rethrown.
1.669 + String constant = negative ? ("-" + nm.substring(index))
1.670 + : nm.substring(index);
1.671 + result = Long.valueOf(constant, radix);
1.672 + }
1.673 + return result;
1.674 + }
1.675 +
1.676 + /**
1.677 + * The value of the {@code Long}.
1.678 + *
1.679 + * @serial
1.680 + */
1.681 + private final long value;
1.682 +
1.683 + /**
1.684 + * Constructs a newly allocated {@code Long} object that
1.685 + * represents the specified {@code long} argument.
1.686 + *
1.687 + * @param value the value to be represented by the
1.688 + * {@code Long} object.
1.689 + */
1.690 + public Long(long value) {
1.691 + this.value = value;
1.692 + }
1.693 +
1.694 + /**
1.695 + * Constructs a newly allocated {@code Long} object that
1.696 + * represents the {@code long} value indicated by the
1.697 + * {@code String} parameter. The string is converted to a
1.698 + * {@code long} value in exactly the manner used by the
1.699 + * {@code parseLong} method for radix 10.
1.700 + *
1.701 + * @param s the {@code String} to be converted to a
1.702 + * {@code Long}.
1.703 + * @throws NumberFormatException if the {@code String} does not
1.704 + * contain a parsable {@code long}.
1.705 + * @see java.lang.Long#parseLong(java.lang.String, int)
1.706 + */
1.707 + public Long(String s) throws NumberFormatException {
1.708 + this.value = parseLong(s, 10);
1.709 + }
1.710 +
1.711 + /**
1.712 + * Returns the value of this {@code Long} as a
1.713 + * {@code byte}.
1.714 + */
1.715 + public byte byteValue() {
1.716 + return (byte)value;
1.717 + }
1.718 +
1.719 + /**
1.720 + * Returns the value of this {@code Long} as a
1.721 + * {@code short}.
1.722 + */
1.723 + public short shortValue() {
1.724 + return (short)value;
1.725 + }
1.726 +
1.727 + /**
1.728 + * Returns the value of this {@code Long} as an
1.729 + * {@code int}.
1.730 + */
1.731 + public int intValue() {
1.732 + return (int)value;
1.733 + }
1.734 +
1.735 + /**
1.736 + * Returns the value of this {@code Long} as a
1.737 + * {@code long} value.
1.738 + */
1.739 + public long longValue() {
1.740 + return (long)value;
1.741 + }
1.742 +
1.743 + /**
1.744 + * Returns the value of this {@code Long} as a
1.745 + * {@code float}.
1.746 + */
1.747 + public float floatValue() {
1.748 + return (float)value;
1.749 + }
1.750 +
1.751 + /**
1.752 + * Returns the value of this {@code Long} as a
1.753 + * {@code double}.
1.754 + */
1.755 + public double doubleValue() {
1.756 + return (double)value;
1.757 + }
1.758 +
1.759 + /**
1.760 + * Returns a {@code String} object representing this
1.761 + * {@code Long}'s value. The value is converted to signed
1.762 + * decimal representation and returned as a string, exactly as if
1.763 + * the {@code long} value were given as an argument to the
1.764 + * {@link java.lang.Long#toString(long)} method.
1.765 + *
1.766 + * @return a string representation of the value of this object in
1.767 + * base 10.
1.768 + */
1.769 + public String toString() {
1.770 + return toString(value);
1.771 + }
1.772 +
1.773 + /**
1.774 + * Returns a hash code for this {@code Long}. The result is
1.775 + * the exclusive OR of the two halves of the primitive
1.776 + * {@code long} value held by this {@code Long}
1.777 + * object. That is, the hashcode is the value of the expression:
1.778 + *
1.779 + * <blockquote>
1.780 + * {@code (int)(this.longValue()^(this.longValue()>>>32))}
1.781 + * </blockquote>
1.782 + *
1.783 + * @return a hash code value for this object.
1.784 + */
1.785 + public int hashCode() {
1.786 + return (int)(value ^ (value >>> 32));
1.787 + }
1.788 +
1.789 + /**
1.790 + * Compares this object to the specified object. The result is
1.791 + * {@code true} if and only if the argument is not
1.792 + * {@code null} and is a {@code Long} object that
1.793 + * contains the same {@code long} value as this object.
1.794 + *
1.795 + * @param obj the object to compare with.
1.796 + * @return {@code true} if the objects are the same;
1.797 + * {@code false} otherwise.
1.798 + */
1.799 + public boolean equals(Object obj) {
1.800 + if (obj instanceof Long) {
1.801 + return value == ((Long)obj).longValue();
1.802 + }
1.803 + return false;
1.804 + }
1.805 +
1.806 + /**
1.807 + * Determines the {@code long} value of the system property
1.808 + * with the specified name.
1.809 + *
1.810 + * <p>The first argument is treated as the name of a system property.
1.811 + * System properties are accessible through the {@link
1.812 + * java.lang.System#getProperty(java.lang.String)} method. The
1.813 + * string value of this property is then interpreted as a
1.814 + * {@code long} value and a {@code Long} object
1.815 + * representing this value is returned. Details of possible
1.816 + * numeric formats can be found with the definition of
1.817 + * {@code getProperty}.
1.818 + *
1.819 + * <p>If there is no property with the specified name, if the
1.820 + * specified name is empty or {@code null}, or if the
1.821 + * property does not have the correct numeric format, then
1.822 + * {@code null} is returned.
1.823 + *
1.824 + * <p>In other words, this method returns a {@code Long} object equal to
1.825 + * the value of:
1.826 + *
1.827 + * <blockquote>
1.828 + * {@code getLong(nm, null)}
1.829 + * </blockquote>
1.830 + *
1.831 + * @param nm property name.
1.832 + * @return the {@code Long} value of the property.
1.833 + * @see java.lang.System#getProperty(java.lang.String)
1.834 + * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
1.835 + */
1.836 + public static Long getLong(String nm) {
1.837 + return getLong(nm, null);
1.838 + }
1.839 +
1.840 + /**
1.841 + * Determines the {@code long} value of the system property
1.842 + * with the specified name.
1.843 + *
1.844 + * <p>The first argument is treated as the name of a system property.
1.845 + * System properties are accessible through the {@link
1.846 + * java.lang.System#getProperty(java.lang.String)} method. The
1.847 + * string value of this property is then interpreted as a
1.848 + * {@code long} value and a {@code Long} object
1.849 + * representing this value is returned. Details of possible
1.850 + * numeric formats can be found with the definition of
1.851 + * {@code getProperty}.
1.852 + *
1.853 + * <p>The second argument is the default value. A {@code Long} object
1.854 + * that represents the value of the second argument is returned if there
1.855 + * is no property of the specified name, if the property does not have
1.856 + * the correct numeric format, or if the specified name is empty or null.
1.857 + *
1.858 + * <p>In other words, this method returns a {@code Long} object equal
1.859 + * to the value of:
1.860 + *
1.861 + * <blockquote>
1.862 + * {@code getLong(nm, new Long(val))}
1.863 + * </blockquote>
1.864 + *
1.865 + * but in practice it may be implemented in a manner such as:
1.866 + *
1.867 + * <blockquote><pre>
1.868 + * Long result = getLong(nm, null);
1.869 + * return (result == null) ? new Long(val) : result;
1.870 + * </pre></blockquote>
1.871 + *
1.872 + * to avoid the unnecessary allocation of a {@code Long} object when
1.873 + * the default value is not needed.
1.874 + *
1.875 + * @param nm property name.
1.876 + * @param val default value.
1.877 + * @return the {@code Long} value of the property.
1.878 + * @see java.lang.System#getProperty(java.lang.String)
1.879 + * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
1.880 + */
1.881 + public static Long getLong(String nm, long val) {
1.882 + Long result = Long.getLong(nm, null);
1.883 + return (result == null) ? Long.valueOf(val) : result;
1.884 + }
1.885 +
1.886 + /**
1.887 + * Returns the {@code long} value of the system property with
1.888 + * the specified name. The first argument is treated as the name
1.889 + * of a system property. System properties are accessible through
1.890 + * the {@link java.lang.System#getProperty(java.lang.String)}
1.891 + * method. The string value of this property is then interpreted
1.892 + * as a {@code long} value, as per the
1.893 + * {@code Long.decode} method, and a {@code Long} object
1.894 + * representing this value is returned.
1.895 + *
1.896 + * <ul>
1.897 + * <li>If the property value begins with the two ASCII characters
1.898 + * {@code 0x} or the ASCII character {@code #}, not followed by
1.899 + * a minus sign, then the rest of it is parsed as a hexadecimal integer
1.900 + * exactly as for the method {@link #valueOf(java.lang.String, int)}
1.901 + * with radix 16.
1.902 + * <li>If the property value begins with the ASCII character
1.903 + * {@code 0} followed by another character, it is parsed as
1.904 + * an octal integer exactly as by the method {@link
1.905 + * #valueOf(java.lang.String, int)} with radix 8.
1.906 + * <li>Otherwise the property value is parsed as a decimal
1.907 + * integer exactly as by the method
1.908 + * {@link #valueOf(java.lang.String, int)} with radix 10.
1.909 + * </ul>
1.910 + *
1.911 + * <p>Note that, in every case, neither {@code L}
1.912 + * (<code>'\u004C'</code>) nor {@code l}
1.913 + * (<code>'\u006C'</code>) is permitted to appear at the end
1.914 + * of the property value as a type indicator, as would be
1.915 + * permitted in Java programming language source code.
1.916 + *
1.917 + * <p>The second argument is the default value. The default value is
1.918 + * returned if there is no property of the specified name, if the
1.919 + * property does not have the correct numeric format, or if the
1.920 + * specified name is empty or {@code null}.
1.921 + *
1.922 + * @param nm property name.
1.923 + * @param val default value.
1.924 + * @return the {@code Long} value of the property.
1.925 + * @see java.lang.System#getProperty(java.lang.String)
1.926 + * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
1.927 + * @see java.lang.Long#decode
1.928 + */
1.929 + public static Long getLong(String nm, Long val) {
1.930 + String v = null;
1.931 + try {
1.932 + v = AbstractStringBuilder.getProperty(nm);
1.933 + } catch (IllegalArgumentException e) {
1.934 + } catch (NullPointerException e) {
1.935 + }
1.936 + if (v != null) {
1.937 + try {
1.938 + return Long.decode(v);
1.939 + } catch (NumberFormatException e) {
1.940 + }
1.941 + }
1.942 + return val;
1.943 + }
1.944 +
1.945 + /**
1.946 + * Compares two {@code Long} objects numerically.
1.947 + *
1.948 + * @param anotherLong the {@code Long} to be compared.
1.949 + * @return the value {@code 0} if this {@code Long} is
1.950 + * equal to the argument {@code Long}; a value less than
1.951 + * {@code 0} if this {@code Long} is numerically less
1.952 + * than the argument {@code Long}; and a value greater
1.953 + * than {@code 0} if this {@code Long} is numerically
1.954 + * greater than the argument {@code Long} (signed
1.955 + * comparison).
1.956 + * @since 1.2
1.957 + */
1.958 + public int compareTo(Long anotherLong) {
1.959 + return compare(this.value, anotherLong.value);
1.960 + }
1.961 +
1.962 + /**
1.963 + * Compares two {@code long} values numerically.
1.964 + * The value returned is identical to what would be returned by:
1.965 + * <pre>
1.966 + * Long.valueOf(x).compareTo(Long.valueOf(y))
1.967 + * </pre>
1.968 + *
1.969 + * @param x the first {@code long} to compare
1.970 + * @param y the second {@code long} to compare
1.971 + * @return the value {@code 0} if {@code x == y};
1.972 + * a value less than {@code 0} if {@code x < y}; and
1.973 + * a value greater than {@code 0} if {@code x > y}
1.974 + * @since 1.7
1.975 + */
1.976 + public static int compare(long x, long y) {
1.977 + return (x < y) ? -1 : ((x == y) ? 0 : 1);
1.978 + }
1.979 +
1.980 +
1.981 + // Bit Twiddling
1.982 +
1.983 + /**
1.984 + * The number of bits used to represent a {@code long} value in two's
1.985 + * complement binary form.
1.986 + *
1.987 + * @since 1.5
1.988 + */
1.989 + public static final int SIZE = 64;
1.990 +
1.991 + /**
1.992 + * Returns a {@code long} value with at most a single one-bit, in the
1.993 + * position of the highest-order ("leftmost") one-bit in the specified
1.994 + * {@code long} value. Returns zero if the specified value has no
1.995 + * one-bits in its two's complement binary representation, that is, if it
1.996 + * is equal to zero.
1.997 + *
1.998 + * @return a {@code long} value with a single one-bit, in the position
1.999 + * of the highest-order one-bit in the specified value, or zero if
1.1000 + * the specified value is itself equal to zero.
1.1001 + * @since 1.5
1.1002 + */
1.1003 + public static long highestOneBit(long i) {
1.1004 + // HD, Figure 3-1
1.1005 + i |= (i >> 1);
1.1006 + i |= (i >> 2);
1.1007 + i |= (i >> 4);
1.1008 + i |= (i >> 8);
1.1009 + i |= (i >> 16);
1.1010 + i |= (i >> 32);
1.1011 + return i - (i >>> 1);
1.1012 + }
1.1013 +
1.1014 + /**
1.1015 + * Returns a {@code long} value with at most a single one-bit, in the
1.1016 + * position of the lowest-order ("rightmost") one-bit in the specified
1.1017 + * {@code long} value. Returns zero if the specified value has no
1.1018 + * one-bits in its two's complement binary representation, that is, if it
1.1019 + * is equal to zero.
1.1020 + *
1.1021 + * @return a {@code long} value with a single one-bit, in the position
1.1022 + * of the lowest-order one-bit in the specified value, or zero if
1.1023 + * the specified value is itself equal to zero.
1.1024 + * @since 1.5
1.1025 + */
1.1026 + public static long lowestOneBit(long i) {
1.1027 + // HD, Section 2-1
1.1028 + return i & -i;
1.1029 + }
1.1030 +
1.1031 + /**
1.1032 + * Returns the number of zero bits preceding the highest-order
1.1033 + * ("leftmost") one-bit in the two's complement binary representation
1.1034 + * of the specified {@code long} value. Returns 64 if the
1.1035 + * specified value has no one-bits in its two's complement representation,
1.1036 + * in other words if it is equal to zero.
1.1037 + *
1.1038 + * <p>Note that this method is closely related to the logarithm base 2.
1.1039 + * For all positive {@code long} values x:
1.1040 + * <ul>
1.1041 + * <li>floor(log<sub>2</sub>(x)) = {@code 63 - numberOfLeadingZeros(x)}
1.1042 + * <li>ceil(log<sub>2</sub>(x)) = {@code 64 - numberOfLeadingZeros(x - 1)}
1.1043 + * </ul>
1.1044 + *
1.1045 + * @return the number of zero bits preceding the highest-order
1.1046 + * ("leftmost") one-bit in the two's complement binary representation
1.1047 + * of the specified {@code long} value, or 64 if the value
1.1048 + * is equal to zero.
1.1049 + * @since 1.5
1.1050 + */
1.1051 + public static int numberOfLeadingZeros(long i) {
1.1052 + // HD, Figure 5-6
1.1053 + if (i == 0)
1.1054 + return 64;
1.1055 + int n = 1;
1.1056 + int x = (int)(i >>> 32);
1.1057 + if (x == 0) { n += 32; x = (int)i; }
1.1058 + if (x >>> 16 == 0) { n += 16; x <<= 16; }
1.1059 + if (x >>> 24 == 0) { n += 8; x <<= 8; }
1.1060 + if (x >>> 28 == 0) { n += 4; x <<= 4; }
1.1061 + if (x >>> 30 == 0) { n += 2; x <<= 2; }
1.1062 + n -= x >>> 31;
1.1063 + return n;
1.1064 + }
1.1065 +
1.1066 + /**
1.1067 + * Returns the number of zero bits following the lowest-order ("rightmost")
1.1068 + * one-bit in the two's complement binary representation of the specified
1.1069 + * {@code long} value. Returns 64 if the specified value has no
1.1070 + * one-bits in its two's complement representation, in other words if it is
1.1071 + * equal to zero.
1.1072 + *
1.1073 + * @return the number of zero bits following the lowest-order ("rightmost")
1.1074 + * one-bit in the two's complement binary representation of the
1.1075 + * specified {@code long} value, or 64 if the value is equal
1.1076 + * to zero.
1.1077 + * @since 1.5
1.1078 + */
1.1079 + public static int numberOfTrailingZeros(long i) {
1.1080 + // HD, Figure 5-14
1.1081 + int x, y;
1.1082 + if (i == 0) return 64;
1.1083 + int n = 63;
1.1084 + y = (int)i; if (y != 0) { n = n -32; x = y; } else x = (int)(i>>>32);
1.1085 + y = x <<16; if (y != 0) { n = n -16; x = y; }
1.1086 + y = x << 8; if (y != 0) { n = n - 8; x = y; }
1.1087 + y = x << 4; if (y != 0) { n = n - 4; x = y; }
1.1088 + y = x << 2; if (y != 0) { n = n - 2; x = y; }
1.1089 + return n - ((x << 1) >>> 31);
1.1090 + }
1.1091 +
1.1092 + /**
1.1093 + * Returns the number of one-bits in the two's complement binary
1.1094 + * representation of the specified {@code long} value. This function is
1.1095 + * sometimes referred to as the <i>population count</i>.
1.1096 + *
1.1097 + * @return the number of one-bits in the two's complement binary
1.1098 + * representation of the specified {@code long} value.
1.1099 + * @since 1.5
1.1100 + */
1.1101 + public static int bitCount(long i) {
1.1102 + // HD, Figure 5-14
1.1103 + i = i - ((i >>> 1) & 0x5555555555555555L);
1.1104 + i = (i & 0x3333333333333333L) + ((i >>> 2) & 0x3333333333333333L);
1.1105 + i = (i + (i >>> 4)) & 0x0f0f0f0f0f0f0f0fL;
1.1106 + i = i + (i >>> 8);
1.1107 + i = i + (i >>> 16);
1.1108 + i = i + (i >>> 32);
1.1109 + return (int)i & 0x7f;
1.1110 + }
1.1111 +
1.1112 + /**
1.1113 + * Returns the value obtained by rotating the two's complement binary
1.1114 + * representation of the specified {@code long} value left by the
1.1115 + * specified number of bits. (Bits shifted out of the left hand, or
1.1116 + * high-order, side reenter on the right, or low-order.)
1.1117 + *
1.1118 + * <p>Note that left rotation with a negative distance is equivalent to
1.1119 + * right rotation: {@code rotateLeft(val, -distance) == rotateRight(val,
1.1120 + * distance)}. Note also that rotation by any multiple of 64 is a
1.1121 + * no-op, so all but the last six bits of the rotation distance can be
1.1122 + * ignored, even if the distance is negative: {@code rotateLeft(val,
1.1123 + * distance) == rotateLeft(val, distance & 0x3F)}.
1.1124 + *
1.1125 + * @return the value obtained by rotating the two's complement binary
1.1126 + * representation of the specified {@code long} value left by the
1.1127 + * specified number of bits.
1.1128 + * @since 1.5
1.1129 + */
1.1130 + public static long rotateLeft(long i, int distance) {
1.1131 + return (i << distance) | (i >>> -distance);
1.1132 + }
1.1133 +
1.1134 + /**
1.1135 + * Returns the value obtained by rotating the two's complement binary
1.1136 + * representation of the specified {@code long} value right by the
1.1137 + * specified number of bits. (Bits shifted out of the right hand, or
1.1138 + * low-order, side reenter on the left, or high-order.)
1.1139 + *
1.1140 + * <p>Note that right rotation with a negative distance is equivalent to
1.1141 + * left rotation: {@code rotateRight(val, -distance) == rotateLeft(val,
1.1142 + * distance)}. Note also that rotation by any multiple of 64 is a
1.1143 + * no-op, so all but the last six bits of the rotation distance can be
1.1144 + * ignored, even if the distance is negative: {@code rotateRight(val,
1.1145 + * distance) == rotateRight(val, distance & 0x3F)}.
1.1146 + *
1.1147 + * @return the value obtained by rotating the two's complement binary
1.1148 + * representation of the specified {@code long} value right by the
1.1149 + * specified number of bits.
1.1150 + * @since 1.5
1.1151 + */
1.1152 + public static long rotateRight(long i, int distance) {
1.1153 + return (i >>> distance) | (i << -distance);
1.1154 + }
1.1155 +
1.1156 + /**
1.1157 + * Returns the value obtained by reversing the order of the bits in the
1.1158 + * two's complement binary representation of the specified {@code long}
1.1159 + * value.
1.1160 + *
1.1161 + * @return the value obtained by reversing order of the bits in the
1.1162 + * specified {@code long} value.
1.1163 + * @since 1.5
1.1164 + */
1.1165 + public static long reverse(long i) {
1.1166 + // HD, Figure 7-1
1.1167 + i = (i & 0x5555555555555555L) << 1 | (i >>> 1) & 0x5555555555555555L;
1.1168 + i = (i & 0x3333333333333333L) << 2 | (i >>> 2) & 0x3333333333333333L;
1.1169 + i = (i & 0x0f0f0f0f0f0f0f0fL) << 4 | (i >>> 4) & 0x0f0f0f0f0f0f0f0fL;
1.1170 + i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL;
1.1171 + i = (i << 48) | ((i & 0xffff0000L) << 16) |
1.1172 + ((i >>> 16) & 0xffff0000L) | (i >>> 48);
1.1173 + return i;
1.1174 + }
1.1175 +
1.1176 + /**
1.1177 + * Returns the signum function of the specified {@code long} value. (The
1.1178 + * return value is -1 if the specified value is negative; 0 if the
1.1179 + * specified value is zero; and 1 if the specified value is positive.)
1.1180 + *
1.1181 + * @return the signum function of the specified {@code long} value.
1.1182 + * @since 1.5
1.1183 + */
1.1184 + public static int signum(long i) {
1.1185 + // HD, Section 2-7
1.1186 + return (int) ((i >> 63) | (-i >>> 63));
1.1187 + }
1.1188 +
1.1189 + /**
1.1190 + * Returns the value obtained by reversing the order of the bytes in the
1.1191 + * two's complement representation of the specified {@code long} value.
1.1192 + *
1.1193 + * @return the value obtained by reversing the bytes in the specified
1.1194 + * {@code long} value.
1.1195 + * @since 1.5
1.1196 + */
1.1197 + public static long reverseBytes(long i) {
1.1198 + i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL;
1.1199 + return (i << 48) | ((i & 0xffff0000L) << 16) |
1.1200 + ((i >>> 16) & 0xffff0000L) | (i >>> 48);
1.1201 + }
1.1202 +
1.1203 + /** use serialVersionUID from JDK 1.0.2 for interoperability */
1.1204 + private static final long serialVersionUID = 4290774380558885855L;
1.1205 +}