class BitMap { private byte[] words;//用一个字节数组来存储 private int capacity;//位图的长度 public BitMap(int capacity) { this.capacity = capacity; words = new byte[((capacity - 1) >> 3) + 1]; } /** * @Title: setBit * @Description: * @param num */ public void setBit(int num) { if (num < 0 || num > capacity) { return; } int index = num >> 3; int position = num & 0x7; words[index] |= (1 << position); } /** * * @Title: contains * @Description: 判断某个数是否存在 * @param num * @return */ public boolean contains(int num) { if (num < 0 || num > capacity) { return false; } int index = num >> 3; int position = num & 0x7; return (words[index] & (1 << position)) != 0; } /** * * @Title: clear * @Description: 清除某个值 * @param num */ public void clear(int num) { if (num < 0 || num > capacity) { return; } int index = num >> 3; int position = num & 0x7; words[index] &= ~(1 << position); } }
本文来自博客园,作者:一只烤鸭朝北走,仅用于技术学习,所有资源都来源于网络,部分是转发,部分是个人总结。欢迎共同学习和转载,转载请在醒目位置标明原文。如有侵权,请留言告知,及时撤除。转载请注明原文链接:https://www.cnblogs.com/wha6239/p/15035021.html
浙公网安备 33010602011771号