• [转载备份]BitMapJAVA实现 断舍离


    public class BitMap {
        //保存数据的
        private byte[] bits;
        
        //能够存储多少数据
        private int capacity;
        
        
        public BitMap(int capacity){
            this.capacity = capacity;
            
            //1bit能存储8个数据,那么capacity数据需要多少个bit呢,capacity/8+1,右移3位相当于除以8
            bits = new byte[(capacity >>3 )+1];
        }
        
        public void add(int num){
            // num/8得到byte[]的index
            int arrayIndex = num >> 3; 
            
            // num%8得到在byte[index]的位置
            int position = num & 0x07; 
            
            //将1左移position后,那个位置自然就是1,然后和以前的数据做|,这样,那个位置就替换成1了。
            bits[arrayIndex] |= 1 << position; 
        }
        
        public boolean contain(int num){
            // num/8得到byte[]的index
            int arrayIndex = num >> 3; 
            
            // num%8得到在byte[index]的位置
            int position = num & 0x07; 
            
            //将1左移position后,那个位置自然就是1,然后和以前的数据做&,判断是否为0即可
            return (bits[arrayIndex] & (1 << position)) !=0; 
        }
        
        public void clear(int num){
            // num/8得到byte[]的index
            int arrayIndex = num >> 3; 
            
            // num%8得到在byte[index]的位置
            int position = num & 0x07; 
            
            //将1左移position后,那个位置自然就是1,然后对取反,再与当前值做&,即可清除当前的位置了.
            bits[arrayIndex] &= ~(1 << position); 
    
        }
        
        public static void main(String[] args) {
            BitMap bitmap = new BitMap(100);
            bitmap.add(7);
            System.out.println("插入7成功");
            
            boolean isexsit = bitmap.contain(7);
            System.out.println("7是否存在:"+isexsit);
            
            bitmap.clear(7);
            isexsit = bitmap.contain(7);
            System.out.println("7是否存在:"+isexsit);
        }
    }
     
    博客地址: https://www.cnblogs.com/java2sap/
    世界丰富多彩,知识天花乱坠。
    ---如果有帮到你,点个赞吧~
  • 相关阅读:
    6. Flask请求和响应
    5. Flask模板
    FW:Software Testing
    What is the difference between modified duration, effective duration and duration?
    How to push master to QA branch in GIT
    FTPS Firewall
    Query performance optimization of Vertica
    (Forward)5 Public Speaking Tips That'll Prepare You for Any Interview
    (转)The remote certificate is invalid according to the validation procedure
    Change
  • 原文地址:https://www.cnblogs.com/java2java/p/15632020.html
Copyright © 2020-2023  润新知