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)); } }
算法-查找bitMap
最新推荐文章于 2024-06-25 21:10:42 发布