1 |
/* Long.java -- object wrapper for long |
/* Long.java -- object wrapper for long |
2 |
Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc. |
Copyright (C) 1998, 1999, 2001, 2002, 2004 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
52 |
* @since 1.0 |
* @since 1.0 |
53 |
* @status updated to 1.4 |
* @status updated to 1.4 |
54 |
*/ |
*/ |
55 |
public final class Long extends Number implements Comparable |
public final class Long extends Number implements Comparable<Long> |
56 |
{ |
{ |
57 |
/** |
/** |
58 |
* Compatible with JDK 1.0.2+. |
* Compatible with JDK 1.0.2+. |
76 |
* <code>Class</code> object. |
* <code>Class</code> object. |
77 |
* @since 1.1 |
* @since 1.1 |
78 |
*/ |
*/ |
79 |
public static final Class TYPE = VMClassLoader.getPrimitiveClass ('J'); |
public static final Class<Long> TYPE = VMClassLoader.getPrimitiveClass ('J'); |
80 |
|
|
81 |
|
/** |
82 |
|
* The number of bits needed to represent a <code>long</code>. |
83 |
|
* @since 1.5 |
84 |
|
*/ |
85 |
|
public static final int SIZE = 64; |
86 |
|
|
87 |
/** |
/** |
88 |
* The immutable value of this Long. |
* The immutable value of this Long. |
288 |
} |
} |
289 |
|
|
290 |
/** |
/** |
291 |
|
* Returns a <code>Long</code> object wrapping the value. |
292 |
|
* |
293 |
|
* @param val the value to wrap |
294 |
|
* @return the <code>Long</code> |
295 |
|
*/ |
296 |
|
public static synchronized Long valueOf(long val) |
297 |
|
{ |
298 |
|
// We aren't required to cache here. We could, though perhaps we |
299 |
|
// ought to consider that as an empirical question. |
300 |
|
return new Long(val); |
301 |
|
} |
302 |
|
|
303 |
|
/** |
304 |
* Convert the specified <code>String</code> into a <code>Long</code>. |
* Convert the specified <code>String</code> into a <code>Long</code>. |
305 |
* The <code>String</code> may represent decimal, hexadecimal, or |
* The <code>String</code> may represent decimal, hexadecimal, or |
306 |
* octal numbers. |
* octal numbers. |
530 |
return compareTo((Long) o); |
return compareTo((Long) o); |
531 |
} |
} |
532 |
|
|
533 |
|
|
534 |
|
/** |
535 |
|
* Return the number of bits set in x. |
536 |
|
* @param x value to examine |
537 |
|
* @since 1.5 |
538 |
|
*/ |
539 |
|
public static int bitCount(long x) |
540 |
|
{ |
541 |
|
// Successively collapse alternating bit groups into a sum. |
542 |
|
x = ((x >> 1) & 0x5555555555555555L) + (x & 0x5555555555555555L); |
543 |
|
x = ((x >> 2) & 0x3333333333333333L) + (x & 0x3333333333333333L); |
544 |
|
int v = (int) ((x >>> 32) + x); |
545 |
|
v = ((v >> 4) & 0x0f0f0f0f) + (v & 0x0f0f0f0f); |
546 |
|
v = ((v >> 8) & 0x00ff00ff) + (v & 0x00ff00ff); |
547 |
|
return ((v >> 16) & 0x0000ffff) + (v & 0x0000ffff); |
548 |
|
} |
549 |
|
|
550 |
|
/** |
551 |
|
* Rotate x to the left by distance bits. |
552 |
|
* @param x the value to rotate |
553 |
|
* @param distance the number of bits by which to rotate |
554 |
|
* @since 1.5 |
555 |
|
*/ |
556 |
|
public static long rotateLeft(long x, int distance) |
557 |
|
{ |
558 |
|
// This trick works because the shift operators implicitly mask |
559 |
|
// the shift count. |
560 |
|
return (x << distance) | (x >>> - distance); |
561 |
|
} |
562 |
|
|
563 |
|
/** |
564 |
|
* Rotate x to the right by distance bits. |
565 |
|
* @param x the value to rotate |
566 |
|
* @param distance the number of bits by which to rotate |
567 |
|
* @since 1.5 |
568 |
|
*/ |
569 |
|
public static int rotateRight(long x, int distance) |
570 |
|
{ |
571 |
|
// This trick works because the shift operators implicitly mask |
572 |
|
// the shift count. |
573 |
|
return (x << - distance) | (x >>> distance); |
574 |
|
} |
575 |
|
|
576 |
|
/** |
577 |
|
* Find the highest set bit in value, and return a new value |
578 |
|
* with only that bit set. |
579 |
|
* @param value the value to examine |
580 |
|
* @since 1.5 |
581 |
|
*/ |
582 |
|
public static long highestOneBit(long value) |
583 |
|
{ |
584 |
|
value |= value >>> 1; |
585 |
|
value |= value >>> 2; |
586 |
|
value |= value >>> 4; |
587 |
|
value |= value >>> 8; |
588 |
|
value |= value >>> 16; |
589 |
|
value |= value >>> 32; |
590 |
|
return value ^ (value >>> 1); |
591 |
|
} |
592 |
|
|
593 |
|
/** |
594 |
|
* Return the number of leading zeros in value. |
595 |
|
* @param value the value to examine |
596 |
|
* @since 1.5 |
597 |
|
*/ |
598 |
|
public static int numberOfLeadingZeros(long value) |
599 |
|
{ |
600 |
|
value |= value >>> 1; |
601 |
|
value |= value >>> 2; |
602 |
|
value |= value >>> 4; |
603 |
|
value |= value >>> 8; |
604 |
|
value |= value >>> 16; |
605 |
|
value |= value >>> 32; |
606 |
|
return bitCount(~value); |
607 |
|
} |
608 |
|
|
609 |
|
/** |
610 |
|
* Find the lowest set bit in value, and return a new value |
611 |
|
* with only that bit set. |
612 |
|
* @param value the value to examine |
613 |
|
* @since 1.5 |
614 |
|
*/ |
615 |
|
public static long lowestOneBit(long value) |
616 |
|
{ |
617 |
|
// Classic assembly trick. |
618 |
|
return value & - value; |
619 |
|
} |
620 |
|
|
621 |
|
/** |
622 |
|
* Find the number of trailing zeros in value. |
623 |
|
* @param value the value to examine |
624 |
|
* @since 1.5 |
625 |
|
*/ |
626 |
|
public static int numberOfTrailingZeros(long value) |
627 |
|
{ |
628 |
|
return bitCount((value & -value) - 1); |
629 |
|
} |
630 |
|
|
631 |
|
/** |
632 |
|
* Return 1 if x is positive, -1 if it is negative, and 0 if it is |
633 |
|
* zero. |
634 |
|
* @param x the value to examine |
635 |
|
* @since 1.5 |
636 |
|
*/ |
637 |
|
public static int signum(long x) |
638 |
|
{ |
639 |
|
return x < 0 ? -1 : (x > 0 ? 1 : 0); |
640 |
|
} |
641 |
|
|
642 |
|
/** |
643 |
|
* Reverse the bytes in val. |
644 |
|
* @since 1.5 |
645 |
|
*/ |
646 |
|
public static long reverseBytes(long val) |
647 |
|
{ |
648 |
|
int hi = Integer.reverseBytes((int) val); |
649 |
|
int lo = Integer.reverseBytes((int) (val >>> 32)); |
650 |
|
return (((long) hi) << 32) | lo; |
651 |
|
} |
652 |
|
|
653 |
|
/** |
654 |
|
* Reverse the bits in val. |
655 |
|
* @since 1.5 |
656 |
|
*/ |
657 |
|
public static long reverse(long val) |
658 |
|
{ |
659 |
|
int hi = Integer.reverse((int) val); |
660 |
|
int lo = Integer.reverse((int) (val >>> 32)); |
661 |
|
return (((long) hi) << 32) | lo; |
662 |
|
} |
663 |
|
|
664 |
/** |
/** |
665 |
* Helper for converting unsigned numbers to String. |
* Helper for converting unsigned numbers to String. |
666 |
* |
* |