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