static int hash(int h) {
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}>>> 是什么运算符
^ 好像是或运算
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}>>> 是什么运算符
^ 好像是或运算
1、>>> 是什么运算符
无符号右移运算符2、^ 好像是或运算
异或运算符
>>>无符号右移
^是异或运算符
不用比较运算符,判断int型的a,b两数的大小的答案:/**
* 不用比较运算符,判断int型的a,b两数的大小.
*
* @author JAVA世纪网(java2000.net, laozizhu.com)
*/
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 2;
String[] buf = { "a=>b", "a < b" };
int id1 = (a - b) >>> 31;
System.out.println(id1); // 1
System.out.println(buf[id1]); // a < b
}
}
>>>无符号右移
^是异或运算符 支持
>>>运算符仅被允许用在整数类型, 并且仅对int 和long 值有效。
如果用在short 或byte 值上, 则在应用>>>之前, 该值将通过带符号的向上类型转换被升级为一个int。有
鉴于此,无符号移位通常已成为符号移位。
^是异或运算符。就是对两个二进制数进行异或算法,没什么好说的。可以自己看点例子。
>>>无符号右移
System.out.println( (1 - 2) >>> 31 );输出结果为 1??
无符号右移对高位统一补0,
右移则视原数最高位来决定补1还是0。7楼的算法我以前也见过
因为int是4个字节有32位,
正数的话不管数有多大右移31位后就剩下符号位0
反之,负数右移31位也剩下符号位1。
这个算法的精华就是这样来确定数组的下标。