src/share/classes/java/lang/Long.java

Print this page
rev 7120 : 8007398: Peformance improvements to Integer and Long string formatting.
Reviewed-by: mdugiou, martin, darcy
Contributed-by: Steven Schlansker <stevenschlansker@gmail.com>, Mike Duigou <mike.duigou@oracle.com>


  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.lang.annotation.Native;
  29 import java.math.*;
  30 

  31 /**
  32  * The {@code Long} class wraps a value of the primitive type {@code
  33  * long} in an object. An object of type {@code Long} contains a
  34  * single field whose type is {@code long}.
  35  *
  36  * <p> In addition, this class provides several methods for converting
  37  * a {@code long} to a {@code String} and a {@code String} to a {@code
  38  * long}, as well as other constants and methods useful when dealing
  39  * with a {@code long}.
  40  *
  41  * <p>Implementation note: The implementations of the "bit twiddling"
  42  * methods (such as {@link #highestOneBit(long) highestOneBit} and
  43  * {@link #numberOfTrailingZeros(long) numberOfTrailingZeros}) are
  44  * based on material from Henry S. Warren, Jr.'s <i>Hacker's
  45  * Delight</i>, (Addison Wesley, 2002).
  46  *
  47  * @author  Lee Boynton
  48  * @author  Arthur van Hoff
  49  * @author  Josh Bloch
  50  * @author  Joseph D. Darcy


 327      *
 328      * <p>If the unsigned magnitude is zero, it is represented by a
 329      * single zero character {@code '0'} ({@code '\u005Cu0030'});
 330      * otherwise, the first character of the representation of the
 331      * unsigned magnitude will not be the zero character. The
 332      * characters {@code '0'} ({@code '\u005Cu0030'}) and {@code
 333      * '1'} ({@code '\u005Cu0031'}) are used as binary digits.
 334      *
 335      * @param   i   a {@code long} to be converted to a string.
 336      * @return  the string representation of the unsigned {@code long}
 337      *          value represented by the argument in binary (base&nbsp;2).
 338      * @see #parseUnsignedLong(String, int)
 339      * @see #toUnsignedString(long, int)
 340      * @since   JDK 1.0.2
 341      */
 342     public static String toBinaryString(long i) {
 343         return toUnsignedString0(i, 1);
 344     }
 345 
 346     /**
 347      * Convert the integer to an unsigned number.





















 348      */
 349     private static String toUnsignedString0(long i, int shift) {
 350         char[] buf = new char[64];
 351         int charPos = 64;
 352         int radix = 1 << shift;
 353         long mask = radix - 1;
 354         do {
 355             buf[--charPos] = Integer.digits[(int)(i & mask)];
 356             i >>>= shift;
 357         } while (i != 0);
 358         return new String(buf, charPos, (64 - charPos));

 359     }
 360 
 361     /**
 362      * Returns a {@code String} object representing the specified
 363      * {@code long}.  The argument is converted to signed decimal
 364      * representation and returned as a string, exactly as if the
 365      * argument and the radix 10 were given as arguments to the {@link
 366      * #toString(long, int)} method.
 367      *
 368      * @param   i   a {@code long} to be converted.
 369      * @return  a string representation of the argument in base&nbsp;10.
 370      */
 371     public static String toString(long i) {
 372         if (i == Long.MIN_VALUE)
 373             return "-9223372036854775808";
 374         int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
 375         char[] buf = new char[size];
 376         getChars(i, size, buf);
 377         return new String(buf, true);
 378     }




  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.lang.annotation.Native;
  29 import java.math.*;
  30 
  31 
  32 /**
  33  * The {@code Long} class wraps a value of the primitive type {@code
  34  * long} in an object. An object of type {@code Long} contains a
  35  * single field whose type is {@code long}.
  36  *
  37  * <p> In addition, this class provides several methods for converting
  38  * a {@code long} to a {@code String} and a {@code String} to a {@code
  39  * long}, as well as other constants and methods useful when dealing
  40  * with a {@code long}.
  41  *
  42  * <p>Implementation note: The implementations of the "bit twiddling"
  43  * methods (such as {@link #highestOneBit(long) highestOneBit} and
  44  * {@link #numberOfTrailingZeros(long) numberOfTrailingZeros}) are
  45  * based on material from Henry S. Warren, Jr.'s <i>Hacker's
  46  * Delight</i>, (Addison Wesley, 2002).
  47  *
  48  * @author  Lee Boynton
  49  * @author  Arthur van Hoff
  50  * @author  Josh Bloch
  51  * @author  Joseph D. Darcy


 328      *
 329      * <p>If the unsigned magnitude is zero, it is represented by a
 330      * single zero character {@code '0'} ({@code '\u005Cu0030'});
 331      * otherwise, the first character of the representation of the
 332      * unsigned magnitude will not be the zero character. The
 333      * characters {@code '0'} ({@code '\u005Cu0030'}) and {@code
 334      * '1'} ({@code '\u005Cu0031'}) are used as binary digits.
 335      *
 336      * @param   i   a {@code long} to be converted to a string.
 337      * @return  the string representation of the unsigned {@code long}
 338      *          value represented by the argument in binary (base&nbsp;2).
 339      * @see #parseUnsignedLong(String, int)
 340      * @see #toUnsignedString(long, int)
 341      * @since   JDK 1.0.2
 342      */
 343     public static String toBinaryString(long i) {
 344         return toUnsignedString0(i, 1);
 345     }
 346 
 347     /**
 348      * Format a long (treated as unsigned) into a String.
 349      * @param val the value to format
 350      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 351      */
 352     static String toUnsignedString0(long val, int shift) {
 353         // assert shift > 0 && shift <=5 : "Illegal shift value";
 354         int mag = Long.SIZE - Long.numberOfLeadingZeros(val);
 355         int chars = Math.max(((mag + (shift - 1)) / shift), 1);
 356         char[] buf = new char[chars];
 357 
 358         formatUnsignedLong(val, shift, buf, 0, chars);
 359         return new String(buf, true);
 360     }
 361 
 362     /**
 363      * Format a long (treated as unsigned) into a character buffer.
 364      * @param val the unsigned long to format
 365      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
 366      * @param buf the character buffer to write to
 367      * @param offset the offset in the destination buffer to start at
 368      * @param len the number of characters to write
 369      * @return the lowest character location used
 370      */
 371      static int formatUnsignedLong(long val, int shift, char[] buf, int offset, int len) {
 372         int charPos = len;

 373         int radix = 1 << shift;
 374         int mask = radix - 1;
 375         do {
 376             buf[offset + --charPos] = Integer.digits[((int) val) & mask];
 377             val >>>= shift;
 378         } while (val != 0 && charPos > 0);
 379 
 380         return charPos;
 381     }
 382 
 383     /**
 384      * Returns a {@code String} object representing the specified
 385      * {@code long}.  The argument is converted to signed decimal
 386      * representation and returned as a string, exactly as if the
 387      * argument and the radix 10 were given as arguments to the {@link
 388      * #toString(long, int)} method.
 389      *
 390      * @param   i   a {@code long} to be converted.
 391      * @return  a string representation of the argument in base&nbsp;10.
 392      */
 393     public static String toString(long i) {
 394         if (i == Long.MIN_VALUE)
 395             return "-9223372036854775808";
 396         int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
 397         char[] buf = new char[size];
 398         getChars(i, size, buf);
 399         return new String(buf, true);
 400     }