省内存,检索效率高,小了32倍
size=10000
int[size]
vs
byte[size/8]
if(size%8==0){
bitMap = new byte[size/8];
}else{
bitMap = new byte[size/8+1];
- package bitmap;
- /**
- *
- * @version 创建时间:2012-8-24 上午08:33:13
- *
- */
- public class BitMap
- {
-
-
- public static void main(String[] args) throws InterruptedException {
- int m=0;
- BitMap hm = new BitMap() ;
- hm.add(m);
- hm.add(1) ;
- hm.add(2);
- hm.add(3);
- hm.add(4);
- hm.add(7);
- System.out.println(hm.contains(7));
- hm.remove(4);
- System.out.println(hm.contains(7));
- }
-
- public BitMap() {
- bytes = new byte[12500000];
- }
-
- public BitMap(int size) {
- bytes = new byte[size];
- }
-
- private byte[] bytes = null;
-
- public void add(int i) {
- int r = i / 8;
- int c = i % 8;
- bytes[r] = (byte) (bytes[r] | (1 << c));
- }
-
- public boolean contains(int i) {
- int r = i / 8;
- int c = i % 8;
- if (((byte) ((bytes[r] >>> c)) & 1) == 1) {
- return true;
- }
- return false;
- }
-
- public void remove(int i) {
- int r = i / 8;
- int c = i % 8;
- byte x=(byte)((1<
- bytes[r]=(byte)(bytes[r] & x);
- }
-
- }
>> 右移。 符号位是0补0,是1补1。 >>>无符号右移。补0。
阅读(1123) | 评论(0) | 转发(0) |