public class BitSet { /*private final static int BITS_PER_WORD = 1 << ADDRESS_BITS_PER_WORD; public BitSet() { initWords(BITS_PER_WORD); } public BitSet(int nbits) { initWords(nbits); } private void initWords(int nbits) { a = new long[wordIndex(nbits - 1) + 1]; }*/ private static long[] a =new long[640]; private final static int ADDRESS_BITS_PER_WORD = 6; public static void main(String[] args) { //test(); /* System.out.println(wordIndex(63)); System.out.println(wordIndex(64)); System.out.println(wordIndex(127)); System.out.println(wordIndex(128)); System.out.println("---------------"); System.out.println(63 / 64); System.out.println(64 /64); System.out.println(127 / 64); System.out.println(128 / 64); */ BitSet bitSet = new BitSet(); bitSet.set(4); // bitSet.clear(4); System.out.println(bitSet.get(4)); } public void clear(int bitIndex) { //取反后在取并 //假如bitIndex =2 0010 取反=1101,并=0 a[wordIndex(bitIndex)] &= ~(1L << bitIndex); } public static void test(){ // BitSet BitSet a = new BitSet(); a.set(1); System.out.println(a.get(1)); System.out.println(a.get(2)); System.out.println("---------------------"); for (int i = 1; i < 320; i++) { if (i < 150) { a.set(i); } if (i == 150) { System.out.println("---150-"); } System.out.println(a.get(i)); } } private static int wordIndex(int bitIndex) { return bitIndex >> ADDRESS_BITS_PER_WORD; } public boolean get(int bitIndex) { //假如bitIndex=2 ; 0010 & 0010 return ((a[wordIndex(bitIndex)] & (1L << bitIndex)) != 0); } public void set(int bit) { a[wordIndex(bit)] |= (1L << bit); /* * //0000 |= 0010 a[0] |= (1 << 1); * System.out.println(a[0]); * //0010 |=0100 a[0] |= (1 << 2); * System.out.println(a[0]); * //0010 |= 1000 a[0] |= (1 << 3); * System.out.println(a[0]); */ }}