【Java】BitMap内存实现

 




import java.util.Arrays;

/**
 * bitmap实现
 * cx
 */
public class CustomizableBitmap {

    private int[] bits;

    private int size;

    private static final int EMPTY = 0x00000000;
    private static final int FULL = 0xffffffff;

    public CustomizableBitmap(int size) {
        this.size = size;
        bits = new int[upward(size)];
    }

    private int upward(int n) {
        return (n >> 5) + ((n & 31) == 0 ? 0 : 1);
    }

    public int downward(int n) {
        return n >> 5;
    }

    public void setBit(int n, int value) {
        if (n >= size) throw new RuntimeException("beyond the boundary.");
        if (value != 0 && value != 1) throw new RuntimeException("value error.");
        int index = downward(n);
        if (value == 1)
            bits[index] = bits[index] | (1 << (31 - n % 32));
        else
            bits[index] = bits[index] & ~(1 << (31 - n % 32));
    }

    public void clear() {
        Arrays.fill(bits, EMPTY);
    }

    public int getBit(int n) {
        if(n >= size) throw new RuntimeException("beyond the boundary.");
        int index = downward(n);
        return (bits[index] >> (31 - n % 32)) & 1;
    }

    public boolean isFULL() {
        for (int i = 0; i < bits.length - 1; i++)
            if (bits[i] != FULL) return false;
        return bits[bits.length - 1] == FULL << (32 - size % 32);
    }

    public void print() {
        for (int bit : bits) System.out.print(intToBinaryString(bit));
        System.out.println();
    }

    private static String intToBinaryString(int number) {
        StringBuilder sb = new StringBuilder(32);
        for (int i = 31; i >= 0; i--) sb.append((number >> i) & 1);
        return sb.toString();
    }

    public static void main(String[] args) {
        int SIZE = 20;
        CustomizableBitmap customizableBitmap = new CustomizableBitmap(SIZE);
        for (int i = 0; i < SIZE; i++) {
            customizableBitmap.setBit(i, 1);
            customizableBitmap.print();
        }
        System.out.println(customizableBitmap.isFULL());
        System.out.println("-----getBit测试------");
        for (int i = 0; i < SIZE; i++) {
            System.out.print(customizableBitmap.getBit(i));
        }
        System.out.println();
        System.out.println("-----------");
        for (int i = SIZE-1; i >= 0; i--) {
            customizableBitmap.setBit(i, 0);
            customizableBitmap.print();
        }
        System.out.println("------getBit测试-----");
        for (int i = 0; i < SIZE; i++) {
            System.out.print(customizableBitmap.getBit(i));
        }
        System.out.println();
        System.out.println("-----------");
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值