Java序列字段和最大值_Java源码序列——数据类型源码

1 基本数据类型

1.0 基本数据类型综述

目前基本数据类型在jdk发布的源码当中共分为了三部分:布尔类型

字符类型

数值类型

注:正好与面经契合

注:jdk源码在src\java.base\java\lang中

1.1 布尔类型源码解析

Boolean关注点:boolean类型可以创建对象,但是参数只有包含一个字段,如new Boolean(true),不可以new Boolean(true,false)

Boolean与String的相互转换

1.1.1 函数头

package java.lang;

import jdk.internal.HotSpotIntrinsicCandidate;//会替代JDK源码实现,获取更高的效率

public final class Boolean implements java.io.Serializable,//实现对象的序列化, // 为了解决 在对 对象流 进行读写操作时所引发的问题(序列化详细介绍) Comparable//两数之间的比较问题{

1.1.2 成员变量

1.1.2.1 成员变量第一部分

/* boolean的成员变量(第一部分)*/

public static final Boolean TRUE = new Boolean(true);

public static final Boolean FALSE = new Boolean(false);

1.1.2.2 成员变量第二部分

//boolean的成员变量(第二部分)------>表示基本类型boolean的class对象 //告诉编译器忽略uncheck的警告信息, //如使用List,ArrayList等未进行参数化产生的警告信息 @SuppressWarnings("unchecked")

public static final Class TYPE = (Class) Class.getPrimitiveClass("boolean");

为什么会是一个TYPE呢?int的包装类是Integer,但是Integer.class ==?== int.class 是绝对不相等的,因为这个是两份字节码文件

但是我自己创建了一个getPrimitiveClass方法,通过这个方法来确定使用什么class文件

注意:这个方法不属于Class

程序

Integer.TYPE==int.class;//YES Integer.TYPE == Integer.class;//ERROR在java中有两种对象:实例对象,如 Boolean aBoolean = new Boolean(true);

Class对象 每一个类只要运行就会产生一个class对象,它包含了与类有关的信息,实例对象就是通过class对象产生的

public static final Class> getPrimitiveClass(String typeName) {

if (typeName.equals("byte"))

return byte.class;

if (typeName.equals("short"))

return short.class;

if (typeName.equals("int"))

return int.class;

if (typeName.equals("long"))

return long.class;

if (typeName.equals("char"))

return char.class;

if (typeName.equals("float"))

return float.class;

if (typeName.equals("double"))

return double.class;

if (typeName.equals("boolean"))

return boolean.class;

if (typeName.equals("void"))

return void.class;

throw new IllegalArgumentException("Not primitive type : " + typeName);

}

1.1.2.3 成员变量第三部分

private final boolean value;

private static final long serialVersionUID = -3665804199014368530L;

1.1.3 构造函数

@Deprecated(since="9")//Deprecated的属性,表示已被弃用的版本 public Boolean(boolean value) {

this.value = value;

}

@Deprecated(since="9")

public Boolean(String s) {

this(parseBoolean(s));//this(true或者其他)调用的就是构造参数 }

构造函数不常用了,因为我有了静态工厂,用它干嘛用?

1.1.4 方法

1.1.4.1 方法————将字符串参数解析为布尔值

/** 作用:将字符串参数解析为布尔值* 如何实现:如果字符串参数不是null,并且忽略大小写,等于字符串true* Example: {@code Boolean.parseBoolean("True")} returns {@code true}.
* Example: {@code Boolean.parseBoolean("yes")} returns {@code false}.*/

public static boolean parseBoolean(String s) {

return ((s != null) && s.equalsIgnoreCase("true"));

}

1.1.4.2 方法————返回boolean对象的值

/*作用:返回boolean对象的值如何实现:直接返回定义好的值就好了-------------|*/ |

@HotSpotIntrinsicCandidate//获取更高的效率 | public boolean booleanValue() { |

return value;-----------------------------|

}

但是这样确实能返回找到的值,但是下面这个方法

会优于构造器实现,同时时空性比较好

@HotSpotIntrinsicCandidate

public static Boolean valueOf(boolean b) {

return (b ? TRUE : FALSE);

}

public static Boolean valueOf(String s) {//会首先将String转换为Boolean return parseBoolean(s) ? TRUE : FALSE;

}

//返回的是String类型 public static String toString(boolean b) {

return b ? "true" : "false";

}

public String toString() {

return value ? "true" : "false";

}

1.1.4.3 方法————hash和equals方法

@Override

public int hashCode() {

return Boolean.hashCode(value);

}

public static int hashCode(boolean value) {

return value ? 1231 : 1237;

}//默认规定好的

public boolean equals(Object obj) {

if (obj instanceof Boolean) { //用来测试一个对象是否为一个类的实例 return value == ((Boolean)obj).booleanValue();

}

return false;

}

1.1.4.4 方法————获取系统Boolean值

/*** 当且仅当以参数命名的系统属性存在,也就是说首先设置一个系统属性的boolean值,当系统属性的值equal(“true”)时,返回true,否则返回false,并且返回值是boolean型*/

public static boolean getBoolean(String name) {

boolean result = false;

try {

result = parseBoolean(System.getProperty(name));//读取JVM中的系统属性 } catch (IllegalArgumentException | NullPointerException e) {

}

return result;

}

例子:

String s1="false";

System.setProperty(s1, "false");//设置一个系统属性值 //对系统属性值判定 boolean aBoolean2 = Boolean.getBoolean(s1);

System.out.println(aBoolean2);

1.1.4.5 方法————比较问题

public int compareTo(Boolean b) {

return compare(this.value, b.value);

}

public static int compare(boolean x, boolean y) {

return (x == y) ? 0 : (x ? 1 : -1);

}

1.1.4.6 方法————逻辑与或非

public static boolean logicalAnd(boolean a, boolean b) {

return a && b;

}

public static boolean logicalOr(boolean a, boolean b) {

return a || b;

}

public static boolean logicalXor(boolean a, boolean b) {

return a ^ b;

}

}

1.2 Number源码解析

1.2.0 总结总共有4个抽象方法,主要作用是在子类当中实现

number是所有整型的父类

number是抽象方法

package java.lang;

public abstract class Number implements java.io.Serializable {

public abstract int intValue();

public abstract long longValue();

public abstract float floatValue();

public abstract double doubleValue();

public byte byteValue() {return (byte)intValue();}

public short shortValue() {return (short)intValue();}

private static final long serialVersionUID = -8742448824652078965L;

}

1.3 Integer源码解析

因为整型可以分为:数值型和浮点型,那么就以Integer作为介绍重点。

1.3.1 函数头

package java.lang;

import java.lang.annotation.Native;

//继承了Number,那么就一定重写了Number的四个方法

public final class Integer extends Number implements Comparable {

1.3.2 成员变量

//使用native关键字说明这个方法是原生函数,也就是这个方法是用C/C++语言实现的,并且被编译成了DLL或者SO(Linux下),由java去调用。

//定义了最小值和最大值 @Native public static final int MIN_VALUE = 0x80000000;//-(2^31) @Native public static final int MAX_VALUE = 0x7fffffff;//(2^31-1)

//字节码文件,同Boolean源码 @SuppressWarnings("unchecked")

public static final Class TYPE = (Class) Class.getPrimitiveClass("int");

//所有可能的将数字表示为字符串的字符集合。 final static char[] digits = {

'0' , '1' , '2' , '3' , '4' , '5' ,

'6' , '7' , '8' , '9' , 'a' , 'b' ,

'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,

'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,

'o' , 'p' , 'q' , 'r' , 's' , 't' ,

'u' , 'v' , 'w' , 'x' , 'y' , 'z'

};

注:字符串转数字的合法字符串分为2部分,1)-或+符号位; 2)0到9,a到z; 符号位不参与转换

基数必须大于1,因为如果基数等于1,那么就会无意义,陷入无限循环。所以,0到9一共10位,a到z一共26位,所以一共36位。

1.3.3 成员方法

1.3.3.1 成员方法一——int类型转换为字符串

例子:

public class NumberSourceExample {

public static void main(String[] args) {

Integer integer = new Integer(1);

String s = Integer.toString(-2, 1);

System.out.println(s);

//验证是不是字符串形式 System.out.println(s+3);

源码: 主题思路就是:首先判断是不是十进制,如果不是就先转换,转换完成之后

将符号位与数字为分割开来(那么如何来转换到String类型呢?----先将int转换到数组(/、%两种途径)中,然后通过数组转换为String)

public static String toString(int i, int radix) {//数字,进制 //1. 判断是不是十进制,如果进制数小于Character.MIN_RADIX或大于Character.MAX_RADIX,则使用10进制 。 /* 为什么要一定保持十进制呢?* 这个的主要原因在于我之后取模取余很好判断* 例子:25/10---->2,5,然后放到了数组,在从数组到字符串就可以了*/

if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)

radix = 10;

if (radix == 10) {

return toString(i);

}

//主要是来放元素 char buf[] = new char[33];

boolean negative = (i < 0);//判断一下引进来的数字是哪个范围的 int charPos = 32;

/*正负数呢,我就通过boolean来判断就好了------>negative如果说是个位数,那么就可以直接放入到数组中---->buf[charPos] = digits[-i];如果说是十位数,那么先进行拆分,然后在进行组合*/

if (!negative) {//negative为假 i = -i;

}

while (i <= -radix) {

buf[charPos--] = digits[-(i % radix)];

i = i / radix;

}

buf[charPos] = digits[-i];

if (negative) {

buf[--charPos] = '-';

}

return new String(buf, charPos, (33 - charPos));

//可以实现数组到字符串的转换 //将value[]数组按照传入的下标和指定的截取数组数据的数量进行截取,并且创建一个内容为此的string对象 //offset表示第一个被截取的字符在数组value[]中的下标,count表示从此字符开始向后截取的字符的数量 }

1.3.3.2 成员方法二——int类型转换为无符号的字符串

// public static String toUnsignedString(int i, int radix) { return Long.toUnsignedString(toUnsignedLong(i), radix); }

//根据int类型转换为无符字符串,根据无符字符串进行设置 //为什么是移位这么多,就是与进制有关系 public static String toHexString(int i) { return toUnsignedString0(i, 4); }

public static String toOctalString(int i) { return toUnsignedString0(i, 3); }

public static String toBinaryString(int i) { return toUnsignedString0(i, 1); }

/*** Convert the integer to an unsigned number.int类型转换为无符号字符串*/

private static String toUnsignedString0(int val, int shift) {//移位符 //Integer.numberOfLeadingZeros(val); //作用为返回无符号整型i的最高非零位前面的0的个数,包括符号位在内。 // 所以mag为val的二进制有效位数 int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);

//chars表示的是var转换为1<

//java9之后,为了节省字符串的空间,默认开启字符串压缩,也就是用byte(8位)保存字母。 // COMPACT_STRINGS默认开启。coder由两个值:LATIN1,UTF16。UTF16是肯定没有开启压缩的。 //COMPACT_STRINGS默认为true即使用的LATIN1 if (COMPACT_STRINGS) {

byte[] buf = new byte[chars];

//该方法就是将val的(1<

return new String(buf, LATIN1);

} else {

byte[] buf = new byte[chars * 2];

formatUnsignedIntUTF16(val, shift, buf, 0, chars);

return new String(buf, UTF16);

}

}

使用的方法一——把一个长类型转换为char数组

static int formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) {

int charPos = len;

int radix = 1 << shift;//进制 int mask = radix - 1;

//先循环后判断 do {

buf[offset + --charPos] = Integer.digits[val & mask];

val >>>= shift;

} while (val != 0 && charPos > 0);

return charPos;

}

使用的方法二——无符号整型i的最高非零位前面的0的个数

//返回无符号整型i的最高非零位前面的0的个数,包括符号位在内 //”>>>”表示无符号右移运算符。高位补0 //采用的是二分法的思想,https://www.jianshu.com/p/2c1be41f6e59 public static int numberOfLeadingZeros(int i) {

// HD, Figure 5-6 if (i == 0)

return 32;

int n = 1;//n作为符号位存在 if (i >>> 16 == 0) { n += 16; i <<= 16; } //i右移16位 左移赋值 if (i >>> 24 == 0) { n += 8; i <<= 8; } //i右移24位 if (i >>> 28 == 0) { n += 4; i <<= 4; } //i右移28位 if (i >>> 30 == 0) { n += 2; i <<= 2; } //i右移30位 n -= i >>> 31;// return n;

}

1.3.3.2 成员方法三——int类型转换为字符串(与方法一相同,但是属于不同思路)

虽然与方法一思路一样,但是用了不同的方法

//两个是转置矩阵,具体用处,确实不了解

final static char [] DigitTens = {

'0', '0', '0', '0', '0', '0', '0', '0', '0', '0',

'1', '1', '1', '1', '1', '1', '1', '1', '1', '1',

'2', '2', '2', '2', '2', '2', '2', '2', '2', '2',

'3', '3', '3', '3', '3', '3', '3', '3', '3', '3',

'4', '4', '4', '4', '4', '4', '4', '4', '4', '4',

'5', '5', '5', '5', '5', '5', '5', '5', '5', '5',

'6', '6', '6', '6', '6', '6', '6', '6', '6', '6',

'7', '7', '7', '7', '7', '7', '7', '7', '7', '7',

'8', '8', '8', '8', '8', '8', '8', '8', '8', '8',

'9', '9', '9', '9', '9', '9', '9', '9', '9', '9',

} ;

final static char [] DigitOnes = {

'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',

'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',

'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',

'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',

'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',

'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',

'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',

'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',

'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',

'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',

} ;

public static String toString(int i) {

if (i == Integer.MIN_VALUE)

return "-2147483648";

int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);//为什么要+1呢,主要原因是负数的话,需要计算符号位 char[] buf = new char[size];

getChars(i, size, buf);

return new String(buf, true);

}

public static String toUnsignedString(int i) {

return Long.toString(toUnsignedLong(i));

}

static void getChars(int i, int index, char[] buf){

int q, r;

int charPos = index;

char sign = 0;

if (i < 0) {

sign = '-';

i = -i;

}

// 当i >= 65536的时候每一次获取两位的char值。 while (i >= 65536) {

q = i / 100;

// really: r = i - (q * 100); r = i - ((q << 6) + (q << 5) + (q << 2));

//使用移位操作快速计算出q*100,2^6+2^5+2^2=64+32+4=100. i = q;

buf [--charPos] = DigitOnes[r];

buf [--charPos] = DigitTens[r];

}

// 当 i <= 65536的时候每次只获取一位的char值 // assert(i <= 65536, i); for (;;) {

q = (i * 52429) >>> (16+3);

//q/10,2^19=524288, (double)52429/(1<<19)=0.10000038146972656 r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ... buf [--charPos] = digits [r];

i = q;

if (i == 0) break;

}

if (sign != 0) {

buf [--charPos] = sign;

//如果是负数加上符号位 }

}

//来获取十进制长度,跟各个十进制的 final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,

99999999, 999999999, Integer.MAX_VALUE };

// Requires positive x static int stringSize(int x) {

for (int i=0; ; i++)

if (x <= sizeTable[i])

return i+1;

}

1.3.3.4 成员方法四——String类型转化为int类型

JDK作者:

写到------->在初始化IntegerCache之前,VM初始化期间可能会提前调用此方法。 必须注意不要使用valueOf方法。

public static int parseInt(String s, int radix) throws NumberFormatException

{

//几种特殊情况的判断 if (s == null) {

throw new NumberFormatException("null");

}

if (radix < Character.MIN_RADIX) {

throw new NumberFormatException("radix " + radix +

" less than Character.MIN_RADIX");

}

if (radix > Character.MAX_RADIX) {

throw new NumberFormatException("radix " + radix +

" greater than Character.MAX_RADIX");

}

int result = 0;

boolean negative = false;

int i = 0, len = s.length();

int limit = -Integer.MAX_VALUE;

int multmin;

int digit;

if (len > 0) {

char firstChar = s.charAt(0);

if (firstChar < '0') { // 主要是判断 if (firstChar == '-') {

negative = true;

limit = Integer.MIN_VALUE;

} else if (firstChar != '+')

throw NumberFormatException.forInputString(s);

if (len == 1) // Cannot have lone "+" or "-" throw NumberFormatException.forInputString(s);

i++;

}

multmin = limit / radix;

while (i < len) {

// Accumulating negatively avoids surprises near MAX_VALUE digit = Character.digit(s.charAt(i++),radix);

if (digit < 0) {

throw NumberFormatException.forInputString(s);

}

if (result < multmin) {

throw NumberFormatException.forInputString(s);

}

result *= radix;

if (result < limit + digit) {

throw NumberFormatException.forInputString(s);

}

result -= digit;

}

} else {

throw NumberFormatException.forInputString(s);

}

return negative ? result : -result;

}

public static int parseInt(String s) throws NumberFormatException {

return parseInt(s,10);

}

public static int parseUnsignedInt(String s, int radix)

throws NumberFormatException {

if (s == null) {

throw new NumberFormatException("null");

}

int len = s.length();

if (len > 0) {

char firstChar = s.charAt(0);

if (firstChar == '-') {

throw new

NumberFormatException(String.format("Illegal leading minus sign " +

"on unsigned string %s.", s));

} else {

if (len <= 5 || // Integer.MAX_VALUE in Character.MAX_RADIX is 6 digits (radix == 10 && len <= 9) ) { // Integer.MAX_VALUE in base 10 is 10 digits return parseInt(s, radix);

} else {

long ell = Long.parseLong(s, radix);

if ((ell & 0xffff_ffff_0000_0000L) == 0) {

return (int) ell;

} else {

throw new

NumberFormatException(String.format("String value %s exceeds " +

"range of unsigned int.", s));

}

}

}

} else {

throw NumberFormatException.forInputString(s);

}

}

public static int parseUnsignedInt(String s) throws NumberFormatException {

return parseUnsignedInt(s, 10);

}

public static Integer valueOf(String s, int radix) throws NumberFormatException {

return Integer.valueOf(parseInt(s,radix));

}

public static Integer valueOf(String s) throws NumberFormatException {

return Integer.valueOf(parseInt(s, 10));

}

1.3.3.5 成员方法五——Integer的缓存机制

private static class IntegerCache {

static final int low = -128;

static final int high;

static final Integer cache[];

static {

// high value may be configured by property int h = 127;

String integerCacheHighPropValue =

sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");

if (integerCacheHighPropValue != null) {

try {

int i = parseInt(integerCacheHighPropValue);

i = Math.max(i, 127);

// Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - (-low) -1);

} catch( NumberFormatException nfe) {

// If the property cannot be parsed into an int, ignore it. }

}

high = h;

cache = new Integer[(high - low) + 1];

int j = low;

for(int k = 0; k < cache.length; k++)

cache[k] = new Integer(j++);

// range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache.high >= 127;

}

private IntegerCache() {}

}

public static Integer valueOf(int i) {

if (i >= IntegerCache.low && i <= IntegerCache.high)

return IntegerCache.cache[i + (-IntegerCache.low)];

return new Integer(i);

}

private final int value;

public Integer(int value) {

this.value = value;

}

public Integer(String s) throws NumberFormatException {

this.value = parseInt(s, 10);

}

public byte byteValue() {

return (byte)value;

}

public short shortValue() {

return (short)value;

}

public int intValue() {

return value;

}

public long longValue() {

return (long)value;

}

public float floatValue() {

return (float)value;

}

public double doubleValue() {

return (double)value;

}

public String toString() {

return toString(value);

}

1.3.3.6 成员方法六——重写方法

@Override

public int hashCode() {

return Integer.hashCode(value);

}

public static int hashCode(int value) {

return value;

}

public boolean equals(Object obj) {

if (obj instanceof Integer) {

return value == ((Integer)obj).intValue();

}

return false;

}

public static Integer getInteger(String nm) {

return getInteger(nm, null);

}

public static Integer getInteger(String nm, int val) {

Integer result = getInteger(nm, null);

return (result == null) ? Integer.valueOf(val) : result;

}

public static Integer getInteger(String nm, Integer val) {

String v = null;

try {

v = System.getProperty(nm);

} catch (IllegalArgumentException | NullPointerException e) {

}

if (v != null) {

try {

return Integer.decode(v);

} catch (NumberFormatException e) {

}

}

return val;

}

public static Integer decode(String nm) throws NumberFormatException {

int radix = 10;

int index = 0;

boolean negative = false;

Integer result;

if (nm.length() == 0)

throw new NumberFormatException("Zero length string");

char firstChar = nm.charAt(0);

// Handle sign, if present if (firstChar == '-') {

negative = true;

index++;

} else if (firstChar == '+')

index++;

// Handle radix specifier, if present if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {

index += 2;

radix = 16;

}

else if (nm.startsWith("#", index)) {

index ++;

radix = 16;

}

else if (nm.startsWith("0", index) && nm.length() > 1 + index) {

index ++;

radix = 8;

}

if (nm.startsWith("-", index) || nm.startsWith("+", index))

throw new NumberFormatException("Sign character in wrong position");

try {

result = Integer.valueOf(nm.substring(index), radix);

result = negative ? Integer.valueOf(-result.intValue()) : result;

} catch (NumberFormatException e) {

// If number is Integer.MIN_VALUE, we'll end up here. The next line // handles this case, and causes any genuine format error to be // rethrown. String constant = negative ? ("-" + nm.substring(index))

: nm.substring(index);

result = Integer.valueOf(constant, radix);

}

return result;

}

public int compareTo(Integer anotherInteger) {

return compare(this.value, anotherInteger.value);

}

public static int compare(int x, int y) {

return (x < y) ? -1 : ((x == y) ? 0 : 1);

}

public static int compareUnsigned(int x, int y) {

return compare(x + MIN_VALUE, y + MIN_VALUE);

}

public static long toUnsignedLong(int x) {

return ((long) x) & 0xffffffffL;

}

public static int divideUnsigned(int dividend, int divisor) {

// In lieu of tricky code, for now just use long arithmetic. return (int)(toUnsignedLong(dividend) / toUnsignedLong(divisor));

}

public static int remainderUnsigned(int dividend, int divisor) {

// In lieu of tricky code, for now just use long arithmetic. return (int)(toUnsignedLong(dividend) % toUnsignedLong(divisor));

}

//整型的长度为32位 @Native public static final int SIZE = 32;

public static final int BYTES = SIZE / Byte.SIZE;

public static int highestOneBit(int i) {

// HD, Figure 3-1 i |= (i >> 1);

i |= (i >> 2);

i |= (i >> 4);

i |= (i >> 8);

i |= (i >> 16);

return i - (i >>> 1);

}

public static int lowestOneBit(int i) {

// HD, Section 2-1 return i & -i;

}

//返回无符号整型i的最高非零位前面的0的个数,包括符号位在内 //”>>>”表示无符号右移运算符。高位补0 //采用的是二分法的思想,https://www.jianshu.com/p/2c1be41f6e59 public static int numberOfLeadingZeros(int i) {

// HD, Figure 5-6 if (i == 0)

return 32;

int n = 1;//n作为符号位存在 if (i >>> 16 == 0) { n += 16; i <<= 16; } //i右移16位 左移赋值 if (i >>> 24 == 0) { n += 8; i <<= 8; } //i右移24位 if (i >>> 28 == 0) { n += 4; i <<= 4; } //i右移28位 if (i >>> 30 == 0) { n += 2; i <<= 2; } //i右移30位 n -= i >>> 31;// return n;

}

public static int numberOfTrailingZeros(int i) {

// HD, Figure 5-14 int y;

if (i == 0) return 32;

int n = 31;

y = i <<16; if (y != 0) { n = n -16; i = y; }

y = i << 8; if (y != 0) { n = n - 8; i = y; }

y = i << 4; if (y != 0) { n = n - 4; i = y; }

y = i << 2; if (y != 0) { n = n - 2; i = y; }

return n - ((i << 1) >>> 31);

}

public static int bitCount(int i) {

// HD, Figure 5-2 i = i - ((i >>> 1) & 0x55555555);

i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);

i = (i + (i >>> 4)) & 0x0f0f0f0f;

i = i + (i >>> 8);

i = i + (i >>> 16);

return i & 0x3f;

}

public static int rotateLeft(int i, int distance) {

return (i << distance) | (i >>> -distance);

}

public static int rotateRight(int i, int distance) {

return (i >>> distance) | (i << -distance);

}

public static int reverse(int i) {

// HD, Figure 7-1 i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555;

i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333;

i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f;

i = (i << 24) | ((i & 0xff00) << 8) |

((i >>> 8) & 0xff00) | (i >>> 24);

return i;

}

public static int signum(int i) {

// HD, Section 2-7 return (i >> 31) | (-i >>> 31);

}

public static int reverseBytes(int i) {

return ((i >>> 24) ) |

((i >> 8) & 0xFF00) |

((i << 8) & 0xFF0000) |

((i << 24));

}

public static int sum(int a, int b) {

return a + b;

}

public static int max(int a, int b) {

return Math.max(a, b);

}

public static int min(int a, int b) {

return Math.min(a, b);

}

@Native private static final long serialVersionUID = 1360826667806852920L;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值