java实现Bitmap算法
来源:互联网 发布:游戏优化差会怎样 编辑:程序博客网 时间:2024/05/21 09:39
package edu.xaut.jzd;public class Test {int numSize = 1000; int arraySize =(int)Math.ceil((double)numSize/32); private int array[] = new int[arraySize]; /** * @param args */ public static void main(String[] args) { //也可以使用bitset Test test = new Test(); test.initBitMap(); int sortArray[] = new int[]{1,4,32,2,6,9}; for(int i=0;i<sortArray.length;i++){ test.set1(sortArray[i]); } for(int i=0;i<test.numSize;i++){ if(test.get(i) !=0){ System.out.print((i)+" "); } } } public void initBitMap(){ for(int i=0;i<array.length;i++){ array[i] = 0; } } public void set1(int pos){ array[pos>>5] = array[pos>>5] | (1 <<(31-pos% 32) ); //给相应位置1 } public int get(int pos){ return array[pos>>5] & (1 <<(31-pos% 32 )); }}