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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章