以下是 JDK 8 中 Long
和 Short
类的源代码概述:
Long
类:
public final class Long extends Number implements Comparable<Long> {
public static final long MIN_VALUE = 0x8000000000000000L;
public static final long MAX_VALUE = 0x7fffffffffffffffL;
public static final int SIZE = 64;
public static final int BYTES = SIZE / Byte.SIZE;
public static final Class<Long> TYPE = (Class<Long>) Class.getPrimitiveClass("long");
private final long value;
public Long(long value) {
this.value = value;
}
public Long(String s) throws NumberFormatException {
this.value = parseLong(s, 10);
}
public static long parseLong(String s, int radix) throws NumberFormatException {
if (s == null) {
throw new NumberFormatException("null");
}
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix + " out of range");
}
long result = 0;
boolean negative = false;
int i = 0, len = s.length();
long limit = -Long.MAX_VALUE;
long multmin;
int digit;
if (len > 0) {
char firstChar = s.charAt(0);
if (firstChar == '-') {
negative = true;
limit = Long.MIN_VALUE;
i++;
} else if (firstChar == '+') {
i++;
}
multmin = limit / radix;
while (i < len) {
digit = Character.digit(s.charAt(i++), radix);
if (digit < 0) {
throw new NumberFormatException(s);
}
if (result < multmin) {
throw new NumberFormatException(s);
}
result *= radix;
if (result < limit + digit) {
throw new NumberFormatException(s);
}
result -= digit;
}
} else {
throw new NumberFormatException(s);
}
return negative ? result : -result;
}
public static long parseLong(String s) throws NumberFormatException {
return parseLong(s, 10);
}
public static String toString(long i, int radix) {
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
radix = 10;
}
if (radix == 10) {
return toString(i);
}
char[] buf = new char[65];
boolean negative = (i < 0);
int charPos = 64;
if (!negative) {
i = -i;
}
while (i <= -radix) {
buf[charPos--] = Long.digits[(int) (-(i % radix))];
i = i / radix;
}
buf[charPos] = Long.digits[(int) (-i)];
if (negative) {
buf[--charPos] = '-';
}
return new String(buf, charPos, (65 - charPos));
}
public static String toHexString(long i) {
return toUnsignedString(i, 4);
}
public static String toOctalString(long i) {
return toUnsignedString(i, 3);
}
public static String toBinaryString(long i) {
return toUnsignedString(i, 1);
}
public static String toString(long i) {
if (i == Long.MIN_VALUE) {
return "-9223372036854775808";
}
int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
char[] buf = new char[size];
getChars(i, size, buf);
return new String(buf);
}
public static String toUnsignedString(long i, int shift) {
int chars = Math.max(((64 - Long.numberOfLeadingZeros(i) + (shift - 1)) / shift), 1);
char[] buf = new char[chars];
formatUnsignedLong(i, shift, buf, 0, chars);
return new String(buf, true);
}
public static String toUnsignedString(long i) {
return toUnsignedString(i, 1);
}
public static int compare(long x, long y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
public static long highestOneBit(long i) {
i |= (i >> 1);
i |= (i >> 2);
i |= (i >> 4);
i |= (i >> 8);
i |= (i >> 16);
i |= (i >> 32);
return i - (i >>> 1);
}
public static long lowestOneBit(long i) {
return i & -i;
}
public static int numberOfLeadingZeros(long i) {
if (i == 0) {
return 64;
}
int n = 1;
int x = (int) (i >>> 32);
if (x == 0) {
n += 32;
x = (int) i;
}
if (x >>> 16 == 0) {
n += 16;
x <<= 16;
}
if (x >>> 24 == 0) {
n += 8;
x <<= 8;
}
if (x >>> 28 == 0) {
n += 4;
x <<= 4;
}
if (x >>> 30 == 0) {
n += 2;
x <<= 2;
}
n -= x >>> 31;
return n;
}
public static int numberOfTrailingZeros(long i) {
int x, y;
if (i == 0) return 64;
int n = 63;
y = (int)i; if (y != 0) { n = n -32; x = y; } else x = (int)(i>>>32);
y = x <<16; if (y != 0) { n = n -16; x = y; }
y = x << 8; if (y != 0) { n = n - 8; x = y; }
y = x << 4; if (y != 0) { n = n - 4; x = y; }
y = x << 2; if (y != 0) { n = n - 2; x = y; }
return n - ((x << 1) >>> 31);
}
public static int bitCount(long i) {
i = i - ((i >>> 1) & 0x5555555555555555L);
i = (i & 0x3333333333333333L) + ((i >>> 2) & 0x3333333333333333L);
i = (i + (i >>> 4)) & 0x0f0f0f0f0f0f0f0fL;
i = i + (i >>> 8);
i = i + (i >>> 16);
i = i + (i >>> 32);
return (int)i & 0x7f;
}
public static long rotateLeft(long i, int distance) {
return (i << distance) | (i >>> -distance);
}
public static long rotateRight(long i, int distance) {
return (i >>> distance) | (i << -distance);
}
public static long reverse(long i) {
i = (i & 0x5555555555555555L) << 1 | (i >>> 1) & 0x5555555555555555L;
i = (i & 0x3333333333333333L) << 2 | (i >>> 2) & 0x3333333333333333L;
i = (i & 0x0f0f0f0f0f0f0f0fL) << 4 | (i >>> 4) & 0x0f0f0f0f0f0f0f0fL;
i = (i << 56) | ((i >>> 8) & 0x000000000000FF00L) |
((i << 8) & 0x00FF000000000000L) | (i >>> 56);
return i;
}
public static int signum(long i) {
return (int) ((i >> 63) | (-i >>> 63));
}
public static long reverseBytes(long i) {
return ((i >>> 56) ) |
((i >> 40) & 0xFF00 ) |
((i >> 24) & 0xFF0000) |
((i >> 8) & 0xFF000000) |
((i << 8) & 0xFF00000000L) |
((i << 24)