Hash
1、概念
散列,哈希,把任意长度的输入通过某种算法(散列)变化成一个固定的长度输出,这个输出的值就是散列值,属于压缩映射,容易产生哈希冲突
常见的Hash算法:直接取余法
2、Hash冲突的解决方法
1、开放寻址:先通过Hash算法进行计算,如果出现Hash冲突,那么在通过另一种算法进行计算,并存放在某一个位置
2、在散列:如果出现Hash算法如果冲突,那么就换一种算法进行计算
3、链接法:如果出现Hash冲突,那么就将这些元素用链表的形式串起来
3、常见的Hash算法(摘要算法)
md4 md5 hash
4、位运算
1、简单案例
Integer date = 4;
String str = Integer.toBinaryString(date);
System.out.println(str);
------------------------------------------------------------------------------------------------
结果:100
2、位与
// 位与 &(1&1=1 1&0=0 0&0=0)
System.out.println("the 4 is " + Integer.toBinaryString(4));
System.out.println("the 6 is " + Integer.toBinaryString(6));
System.out.println("the 4 & 6 is " + Integer.toBinaryString(4 & 6));
------------------------------------------------------------------------------------------------
结果:the 4 is 100
the 6 is 110
the 4 & 6 is 100
3、位或
// 位或 &(1|1=1 1|0=0 0|0=0)
System.out.println("the 4 is " + Integer.toBinaryString(4));
System.out.println("the 6 is " + Integer.toBinaryString(6));
System.out.println("the 4 | 6 is " + Integer.toBinaryString(4 |6));
------------------------------------------------------------------------------------------------
结果:the 4 is 100
the 6 is 110
the 4 | 6 is 110
4、位非
// 位非 |(~1=0 ~0=1)
System.out.println("the 4 is " + Integer.toBinaryString(4));
System.out.println("the ~4 is " + Integer.toBinaryString(~4));
------------------------------------------------------------------------------------------------
结果:the 4 is 100
the ~4 is 11111111111111111111111111111011
5、位异或
// 位非^(1^1=0 1^0=1 0^0=0)
System.out.println("the 4 is " + Integer.toBinaryString(4));
System.out.println("the 6 is " + Integer.toBinaryString(6));
System.out.println("the 4 ^ 6 is " + Integer.toBinaryString(4 ^ 6));
------------------------------------------------------------------------------------------------
结果:the 4 is 100
the 6 is 110
the 4 ^ 6 is 10
6、左移和右移
// 有符号左移 << 有符号的右移 >> 无符号的右移 >>>
System.out.println("the 44 >> 2 is " + Integer.toBinaryString(4 >> 2));
System.out.println("the 44 << 2 is " + Integer.toBinaryString(4 << 2));
System.out.println("the 44 >>> 2 is " + Integer.toBinaryString(4 >>> 2));
------------------------------------------------------------------------------------------------
结果:the 4 >> 2 is 1
the 4 << 2 is 10000
the 4 >>> 2 is 1
7、补充知识
取模操作 a % (z^n) 等价于 a & (2^n - 1)
System.out.println("the 345 % 16 is " + Integer.toBinaryString(345 % 16));
System.out.println("the 345 & (16 - 1) is " + Integer.toBinaryString(345 & (16 - 1)));
------------------------------------------------------------------------------------------------
结果:the 345 % 16 is 1001
the 345 & (16 - 1) is 1001
版权声明:本文为qq_38242314原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。