1 7 8 package java.lang; 9 10 34 public final class Long extends Number implements Comparable <Long > { 35 39 public static final long MIN_VALUE = 0x8000000000000000L; 40 41 45 public static final long MAX_VALUE = 0x7fffffffffffffffL; 46 47 53 public static final Class <Long > TYPE = (Class <Long >) Class.getPrimitiveClass("long"); 54 55 96 public static String toString(long i, int radix) { 97 if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) 98 radix = 10; 99 if (radix == 10) 100 return toString(i); 101 char[] buf = new char[65]; 102 int charPos = 64; 103 boolean negative = (i < 0); 104 105 if (!negative) { 106 i = -i; 107 } 108 109 while (i <= -radix) { 110 buf[charPos--] = Integer.digits[(int)(-(i % radix))]; 111 i = i / radix; 112 } 113 buf[charPos] = Integer.digits[(int)(-i)]; 114 115 if (negative) { 116 buf[--charPos] = '-'; 117 } 118 119 return new String (buf, charPos, (65 - charPos)); 120 } 121 122 154 public static String toHexString(long i) { 155 return toUnsignedString(i, 4); 156 } 157 158 185 public static String toOctalString(long i) { 186 return toUnsignedString(i, 3); 187 } 188 189 210 public static String toBinaryString(long i) { 211 return toUnsignedString(i, 1); 212 } 213 214 217 private static String toUnsignedString(long i, int shift) { 218 char[] buf = new char[64]; 219 int charPos = 64; 220 int radix = 1 << shift; 221 long mask = radix - 1; 222 do { 223 buf[--charPos] = Integer.digits[(int)(i & mask)]; 224 i >>>= shift; 225 } while (i != 0); 226 return new String (buf, charPos, (64 - charPos)); 227 } 228 229 239 public static String toString(long i) { 240 if (i == Long.MIN_VALUE) 241 return "-9223372036854775808"; 242 int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); 243 char[] buf = new char[size]; 244 getChars(i, size, buf); 245 return new String (0, size, buf); 246 } 247 248 257 static void getChars(long i, int index, char[] buf) { 258 long q; 259 int r; 260 int charPos = index; 261 char sign = 0; 262 263 if (i < 0) { 264 sign = '-'; 265 i = -i; 266 } 267 268 while (i > Integer.MAX_VALUE) { 270 q = i / 100; 271 r = (int)(i - ((q << 6) + (q << 5) + (q << 2))); 273 i = q; 274 buf[--charPos] = Integer.DigitOnes[r]; 275 buf[--charPos] = Integer.DigitTens[r]; 276 } 277 278 int q2; 280 int i2 = (int)i; 281 while (i2 >= 65536) { 282 q2 = i2 / 100; 283 r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2)); 285 i2 = q2; 286 buf[--charPos] = Integer.DigitOnes[r]; 287 buf[--charPos] = Integer.DigitTens[r]; 288 } 289 290 for (;;) { 293 q2 = (i2 * 52429) >>> (16+3); 294 r = i2 - ((q2 << 3) + (q2 << 1)); buf[--charPos] = Integer.digits[r]; 296 i2 = q2; 297 if (i2 == 0) break; 298 } 299 if (sign != 0) { 300 buf[--charPos] = sign; 301 } 302 } 303 304 static int stringSize(long x) { 306 long p = 10; 307 for (int i=1; i<19; i++) { 308 if (x < p) 309 return i; 310 p = 10*p; 311 } 312 return 19; 313 } 314 315 368 public static long parseLong(String s, int radix) 369 throws NumberFormatException 370 { 371 if (s == null) { 372 throw new NumberFormatException ("null"); 373 } 374 375 if (radix < Character.MIN_RADIX) { 376 throw new NumberFormatException ("radix " + radix + 377 " less than Character.MIN_RADIX"); 378 } 379 if (radix > Character.MAX_RADIX) { 380 throw new NumberFormatException ("radix " + radix + 381 " greater than Character.MAX_RADIX"); 382 } 383 384 long result = 0; 385 boolean negative = false; 386 int i = 0, max = s.length(); 387 long limit; 388 long multmin; 389 int digit; 390 391 if (max > 0) { 392 if (s.charAt(0) == '-') { 393 negative = true; 394 limit = Long.MIN_VALUE; 395 i++; 396 } else { 397 limit = -Long.MAX_VALUE; 398 } 399 multmin = limit / radix; 400 if (i < max) { 401 digit = Character.digit(s.charAt(i++),radix); 402 if (digit < 0) { 403 throw NumberFormatException.forInputString(s); 404 } else { 405 result = -digit; 406 } 407 } 408 while (i < max) { 409 digit = Character.digit(s.charAt(i++),radix); 411 if (digit < 0) { 412 throw NumberFormatException.forInputString(s); 413 } 414 if (result < multmin) { 415 throw NumberFormatException.forInputString(s); 416 } 417 result *= radix; 418 if (result < limit + digit) { 419 throw NumberFormatException.forInputString(s); 420 } 421 result -= digit; 422 } 423 } else { 424 throw NumberFormatException.forInputString(s); 425 } 426 if (negative) { 427 if (i > 1) { 428 return result; 429 } else { 430 throw NumberFormatException.forInputString(s); 431 } 432 } else { 433 return -result; 434 } 435 } 436 437 460 public static long parseLong(String s) throws NumberFormatException { 461 return parseLong(s, 10); 462 } 463 464 490 public static Long valueOf(String s, int radix) throws NumberFormatException { 491 return new Long (parseLong(s, radix)); 492 } 493 494 516 public static Long valueOf(String s) throws NumberFormatException 517 { 518 return new Long (parseLong(s, 10)); 519 } 520 521 private static class LongCache { 522 private LongCache(){} 523 524 static final Long cache[] = new Long [-(-128) + 127 + 1]; 525 526 static { 527 for(int i = 0; i < cache.length; i++) 528 cache[i] = new Long (i - 128); 529 } 530 } 531 532 545 public static Long valueOf(long l) { 546 final int offset = 128; 547 if (l >= -128 && l <= 127) { return LongCache.cache[(int)l + offset]; 549 } 550 return new Long (l); 551 } 552 553 596 public static Long decode(String nm) throws NumberFormatException { 597 int radix = 10; 598 int index = 0; 599 boolean negative = false; 600 Long result; 601 602 if (nm.startsWith("-")) { 604 negative = true; 605 index++; 606 } 607 608 if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) { 610 index += 2; 611 radix = 16; 612 } 613 else if (nm.startsWith("#", index)) { 614 index ++; 615 radix = 16; 616 } 617 else if (nm.startsWith("0", index) && nm.length() > 1 + index) { 618 index ++; 619 radix = 8; 620 } 621 622 if (nm.startsWith("-", index)) 623 throw new NumberFormatException ("Negative sign in wrong position"); 624 625 try { 626 result = Long.valueOf(nm.substring(index), radix); 627 result = negative ? new Long ((long)-result.longValue()) : result; 628 } catch (NumberFormatException e) { 629 String constant = negative ? new String ("-" + nm.substring(index)) 633 : nm.substring(index); 634 result = Long.valueOf(constant, radix); 635 } 636 return result; 637 } 638 639 644 private final long value; 645 646 653 public Long(long value) { 654 this.value = value; 655 } 656 657 670 public Long(String s) throws NumberFormatException { 671 this.value = parseLong(s, 10); 672 } 673 674 678 public byte byteValue() { 679 return (byte)value; 680 } 681 682 686 public short shortValue() { 687 return (short)value; 688 } 689 690 694 public int intValue() { 695 return (int)value; 696 } 697 698 702 public long longValue() { 703 return (long)value; 704 } 705 706 710 public float floatValue() { 711 return (float)value; 712 } 713 714 718 public double doubleValue() { 719 return (double)value; 720 } 721 722 732 public String toString() { 733 return String.valueOf(value); 734 } 735 736 747 public int hashCode() { 748 return (int)(value ^ (value >>> 32)); 749 } 750 751 761 public boolean equals(Object obj) { 762 if (obj instanceof Long ) { 763 return value == ((Long )obj).longValue(); 764 } 765 return false; 766 } 767 768 797 public static Long getLong(String nm) { 798 return getLong(nm, null); 799 } 800 801 838 public static Long getLong(String nm, long val) { 839 Long result = Long.getLong(nm, null); 840 return (result == null) ? new Long (val) : result; 841 } 842 843 885 public static Long getLong(String nm, Long val) { 886 String v = null; 887 try { 888 v = System.getProperty(nm); 889 } catch (IllegalArgumentException e) { 890 } catch (NullPointerException e) { 891 } 892 if (v != null) { 893 try { 894 return Long.decode(v); 895 } catch (NumberFormatException e) { 896 } 897 } 898 return val; 899 } 900 901 914 public int compareTo(Long anotherLong) { 915 long thisVal = this.value; 916 long anotherVal = anotherLong.value; 917 return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1)); 918 } 919 920 921 923 929 public static final int SIZE = 64; 930 931 943 public static long highestOneBit(long i) { 944 i |= (i >> 1); 946 i |= (i >> 2); 947 i |= (i >> 4); 948 i |= (i >> 8); 949 i |= (i >> 16); 950 i |= (i >> 32); 951 return i - (i >>> 1); 952 } 953 954 966 public static long lowestOneBit(long i) { 967 return i & -i; 969 } 970 971 991 public static int numberOfLeadingZeros(long i) { 992 if (i == 0) 994 return 64; 995 int n = 1; 996 int x = (int)(i >>> 32); 997 if (x == 0) { n += 32; x = (int)i; } 998 if (x >>> 16 == 0) { n += 16; x <<= 16; } 999 if (x >>> 24 == 0) { n += 8; x <<= 8; } 1000 if (x >>> 28 == 0) { n += 4; x <<= 4; } 1001 if (x >>> 30 == 0) { n += 2; x <<= 2; } 1002 n -= x >>> 31; 1003 return n; 1004 } 1005 1006 1019 public static int numberOfTrailingZeros(long i) { 1020 int x, y; 1022 if (i == 0) return 64; 1023 int n = 63; 1024 y = (int)i; if (y != 0) { n = n -32; x = y; } else x = (int)(i>>>32); 1025 y = x <<16; if (y != 0) { n = n -16; x = y; } 1026 y = x << 8; if (y != 0) { n = n - 8; x = y; } 1027 y = x << 4; if (y != 0) { n = n - 4; x = y; } 1028 y = x << 2; if (y != 0) { n = n - 2; x = y; } 1029 return n - ((x << 1) >>> 31); 1030 } 1031 1032 1041 public static int bitCount(long i) { 1042 i = i - ((i >>> 1) & 0x5555555555555555L); 1044 i = (i & 0x3333333333333333L) + ((i >>> 2) & 0x3333333333333333L); 1045 i = (i + (i >>> 4)) & 0x0f0f0f0f0f0f0f0fL; 1046 i = i + (i >>> 8); 1047 i = i + (i >>> 16); 1048 i = i + (i >>> 32); 1049 return (int)i & 0x7f; 1050 } 1051 1052 1070 public static long rotateLeft(long i, int distance) { 1071 return (i << distance) | (i >>> -distance); 1072 } 1073 1074 1092 public static long rotateRight(long i, int distance) { 1093 return (i >>> distance) | (i << -distance); 1094 } 1095 1096 1105 public static long reverse(long i) { 1106 i = (i & 0x5555555555555555L) << 1 | (i >>> 1) & 0x5555555555555555L; 1108 i = (i & 0x3333333333333333L) << 2 | (i >>> 2) & 0x3333333333333333L; 1109 i = (i & 0x0f0f0f0f0f0f0f0fL) << 4 | (i >>> 4) & 0x0f0f0f0f0f0f0f0fL; 1110 i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL; 1111 i = (i << 48) | ((i & 0xffff0000L) << 16) | 1112 ((i >>> 16) & 0xffff0000L) | (i >>> 48); 1113 return i; 1114 } 1115 1116 1124 public static int signum(long i) { 1125 return (int) ((i >> 63) | (-i >>> 63)); 1127 } 1128 1129 1137 public static long reverseBytes(long i) { 1138 i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL; 1139 return (i << 48) | ((i & 0xffff0000L) << 16) | 1140 ((i >>> 16) & 0xffff0000L) | (i >>> 48); 1141 } 1142 1143 1144 private static final long serialVersionUID = 4290774380558885855L; 1145} 1146 | Popular Tags |