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

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  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.math.*;
  29 
  30 /**
  31  * The {@code Long} class wraps a value of the primitive type {@code
  32  * long} in an object. An object of type {@code Long} contains a
  33  * single field whose type is {@code long}.
  34  *
  35  * <p> In addition, this class provides several methods for converting
  36  * a {@code long} to a {@code String} and a {@code String} to a {@code
  37  * long}, as well as other constants and methods useful when dealing
  38  * with a {@code long}.
  39  *
  40  * <p>Implementation note: The implementations of the "bit twiddling"
  41  * methods (such as {@link #highestOneBit(long) highestOneBit} and
  42  * {@link #numberOfTrailingZeros(long) numberOfTrailingZeros}) are
  43  * based on material from Henry S. Warren, Jr.'s <i>Hacker's
  44  * Delight</i>, (Addison Wesley, 2002).
  45  *
  46  * @author  Lee Boynton
  47  * @author  Arthur van Hoff
  48  * @author  Josh Bloch
  49  * @author  Joseph D. Darcy
  50  * @since   JDK1.0
  51  */
  52 public final class Long extends Number implements Comparable<Long> {
  53     /**
  54      * A constant holding the minimum value a {@code long} can
  55      * have, -2<sup>63</sup>.
  56      */
  57     public static final long MIN_VALUE = 0x8000000000000000L;
  58 
  59     /**
  60      * A constant holding the maximum value a {@code long} can
  61      * have, 2<sup>63</sup>-1.
  62      */
  63     public static final long MAX_VALUE = 0x7fffffffffffffffL;
  64 
  65     /**
  66      * The {@code Class} instance representing the primitive type
  67      * {@code long}.
  68      *
  69      * @since   JDK1.1
  70      */
  71     @SuppressWarnings("unchecked")
  72     public static final Class<Long>     TYPE = (Class<Long>) Class.getPrimitiveClass("long");
  73 
  74     /**
  75      * Returns a string representation of the first argument in the
  76      * radix specified by the second argument.
  77      *
  78      * <p>If the radix is smaller than {@code Character.MIN_RADIX}
  79      * or larger than {@code Character.MAX_RADIX}, then the radix
  80      * {@code 10} is used instead.
  81      *
  82      * <p>If the first argument is negative, the first element of the
  83      * result is the ASCII minus sign {@code '-'}


1300     public static long remainderUnsigned(long dividend, long divisor) {
1301         if (dividend > 0 && divisor > 0) { // signed comparisons
1302             return dividend % divisor;
1303         } else {
1304             if (compareUnsigned(dividend, divisor) < 0) // Avoid explicit check for 0 divisor
1305                 return dividend;
1306             else
1307                 return toUnsignedBigInteger(dividend).
1308                     remainder(toUnsignedBigInteger(divisor)).longValue();
1309         }
1310     }
1311 
1312     // Bit Twiddling
1313 
1314     /**
1315      * The number of bits used to represent a {@code long} value in two's
1316      * complement binary form.
1317      *
1318      * @since 1.5
1319      */
1320     public static final int SIZE = 64;
1321 
1322     /**
1323      * The number of bytes used to represent a {@code long} value in two's
1324      * complement binary form.
1325      *
1326      * @since 1.8
1327      */
1328     public static final int BYTES = SIZE / Byte.SIZE;
1329 
1330     /**
1331      * Returns a {@code long} value with at most a single one-bit, in the
1332      * position of the highest-order ("leftmost") one-bit in the specified
1333      * {@code long} value.  Returns zero if the specified value has no
1334      * one-bits in its two's complement binary representation, that is, if it
1335      * is equal to zero.
1336      *
1337      * @return a {@code long} value with a single one-bit, in the position
1338      *     of the highest-order one-bit in the specified value, or zero if
1339      *     the specified value is itself equal to zero.
1340      * @since 1.5


1523     public static int signum(long i) {
1524         // HD, Section 2-7
1525         return (int) ((i >> 63) | (-i >>> 63));
1526     }
1527 
1528     /**
1529      * Returns the value obtained by reversing the order of the bytes in the
1530      * two's complement representation of the specified {@code long} value.
1531      *
1532      * @return the value obtained by reversing the bytes in the specified
1533      *     {@code long} value.
1534      * @since 1.5
1535      */
1536     public static long reverseBytes(long i) {
1537         i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL;
1538         return (i << 48) | ((i & 0xffff0000L) << 16) |
1539             ((i >>> 16) & 0xffff0000L) | (i >>> 48);
1540     }
1541 
1542     /** use serialVersionUID from JDK 1.0.2 for interoperability */
1543     private static final long serialVersionUID = 4290774380558885855L;
1544 }


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  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
  51  * @since   JDK1.0
  52  */
  53 public final class Long extends Number implements Comparable<Long> {
  54     /**
  55      * A constant holding the minimum value a {@code long} can
  56      * have, -2<sup>63</sup>.
  57      */
  58     @Native public static final long MIN_VALUE = 0x8000000000000000L;
  59 
  60     /**
  61      * A constant holding the maximum value a {@code long} can
  62      * have, 2<sup>63</sup>-1.
  63      */
  64     @Native public static final long MAX_VALUE = 0x7fffffffffffffffL;
  65 
  66     /**
  67      * The {@code Class} instance representing the primitive type
  68      * {@code long}.
  69      *
  70      * @since   JDK1.1
  71      */
  72     @SuppressWarnings("unchecked")
  73     public static final Class<Long>     TYPE = (Class<Long>) Class.getPrimitiveClass("long");
  74 
  75     /**
  76      * Returns a string representation of the first argument in the
  77      * radix specified by the second argument.
  78      *
  79      * <p>If the radix is smaller than {@code Character.MIN_RADIX}
  80      * or larger than {@code Character.MAX_RADIX}, then the radix
  81      * {@code 10} is used instead.
  82      *
  83      * <p>If the first argument is negative, the first element of the
  84      * result is the ASCII minus sign {@code '-'}


1301     public static long remainderUnsigned(long dividend, long divisor) {
1302         if (dividend > 0 && divisor > 0) { // signed comparisons
1303             return dividend % divisor;
1304         } else {
1305             if (compareUnsigned(dividend, divisor) < 0) // Avoid explicit check for 0 divisor
1306                 return dividend;
1307             else
1308                 return toUnsignedBigInteger(dividend).
1309                     remainder(toUnsignedBigInteger(divisor)).longValue();
1310         }
1311     }
1312 
1313     // Bit Twiddling
1314 
1315     /**
1316      * The number of bits used to represent a {@code long} value in two's
1317      * complement binary form.
1318      *
1319      * @since 1.5
1320      */
1321     @Native public static final int SIZE = 64;
1322 
1323     /**
1324      * The number of bytes used to represent a {@code long} value in two's
1325      * complement binary form.
1326      *
1327      * @since 1.8
1328      */
1329     public static final int BYTES = SIZE / Byte.SIZE;
1330 
1331     /**
1332      * Returns a {@code long} value with at most a single one-bit, in the
1333      * position of the highest-order ("leftmost") one-bit in the specified
1334      * {@code long} value.  Returns zero if the specified value has no
1335      * one-bits in its two's complement binary representation, that is, if it
1336      * is equal to zero.
1337      *
1338      * @return a {@code long} value with a single one-bit, in the position
1339      *     of the highest-order one-bit in the specified value, or zero if
1340      *     the specified value is itself equal to zero.
1341      * @since 1.5


1524     public static int signum(long i) {
1525         // HD, Section 2-7
1526         return (int) ((i >> 63) | (-i >>> 63));
1527     }
1528 
1529     /**
1530      * Returns the value obtained by reversing the order of the bytes in the
1531      * two's complement representation of the specified {@code long} value.
1532      *
1533      * @return the value obtained by reversing the bytes in the specified
1534      *     {@code long} value.
1535      * @since 1.5
1536      */
1537     public static long reverseBytes(long i) {
1538         i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL;
1539         return (i << 48) | ((i & 0xffff0000L) << 16) |
1540             ((i >>> 16) & 0xffff0000L) | (i >>> 48);
1541     }
1542 
1543     /** use serialVersionUID from JDK 1.0.2 for interoperability */
1544     @Native private static final long serialVersionUID = 4290774380558885855L;
1545 }