Hash入门和位运算

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值