算法-查找bitMap

package com.redis.hash;

/**
 * 计算机当中最小的单位是:Bit位
 * 1Byte = 8bit
 * int = 4byte= 4*8bit
 * flote = 4Byte
 * Long = 8Byte
 * Char = 2Byte
 *
 * Int a = 1;在计算机当中怎么个存储的?
 * 0000 0000 0000 0000 0000 0000 0000 0001
 * 有什么用呢?
 * 运算符:  左移
 *  2<<1 =2*2
 *  2<<2 = 2*4
 *  是怎么来的?怎么变化的?
 *   8<<2 =
 *   8在二进制当中的表示:
 *    0000 0000 0000 0000 0000 0000 0000 1000  //int类型的32位bit
 *  //左移两位,但是总体的32位不变,左边的两位删掉,掉进坑里,右边的补齐
 *    00 0000 0000 0000 0000 0000 0000 100000    //10进制转化为2进制  2^5 = 32
 *   //右移两位
 *   0000 0000 0000 0000 0000 0000 0000 1000
 *     0000 0000 0000 0000 0000 0000 0000 10    //1后面有几位就是2的几次方  2^1 = 2
 *   >>> 无符号右移
 *
 *   位与& :同位上的两个数都是1,则为1,否则0;
 *   位与|: 同位上的只要有一个数是1,则为1,否则0;
 *  比如:
 *   8/4 =>8>>2
 *   8*4 =>8<<2
 *   1.注意8在计算机当中存储的基本位置
 *   2亿数据占有的内存:
 *     2亿*4(byte)/1024/1024 = 762M
 *     转化为Bit :762M/32 = 23M
 */
public class BitMap {

    //属性值
    byte[] bytes;   //用byte表示,存储的数据的形式
    int max;   //最大的数;
    //初始化对象
    public BitMap(int max) {
        this.max = max;
        bytes = new byte[(max>>3)+1];   //max/8 +1;
    }
//    操作
    public void add(int  n){  //往bitMap中添加数据
        int bitIndex = n >>3;  //可以知道那个byte
        int loc = n % 8;    //存储的位置
        bytes[bitIndex] |=1<<3;
    }

    public boolean find(int n){
        int bitIndex = n >>3;  //可以知道那个byte
        int loc = n % 8;    //存储的位置
        int flag = bytes[bitIndex] & (1<<loc);
        if (flag == 0){
            return  false;
        }
        return  true;
    }

    /**
     * 1.数据判重
     * 2.没有重复的数据排序
     * 3.
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(2 % 32);
        System.out.println(2 / 32);
        BitMap bitMap = new BitMap(100);
        bitMap.add(2);
        bitMap.add(3);
        bitMap.add(65);
        bitMap.add(66);
        System.out.println(bitMap.find(3));
        System.out.println(bitMap.find(64));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值