复制代码
- //:Bits.java
- //DemonstrationofBitSet
- importjava.util.*;
- publicclassBits{
- publicstaticvoidmain(String[]args){
- Randomrand=newRandom();
- //TaketheLSBofnextInt():
- bytebt=(byte)rand.nextInt();
- BitSetbb=newBitSet();
- for(inti=7;i>=0;i--)
- if(((1<<i)&bt)!=0)
- bb.set(i);
- else
- bb.clear(i);
- System.out.println("bytevalue:"+bt);
- printBitSet(bb);
- shortst=(short)rand.nextInt();
- BitSetbs=newBitSet();
- for(inti=15;i>=0;i--)
- if(((1<<i)&st)!=0)
- bs.set(i);
- else
- bs.clear(i);
- System.out.println("shortvalue:"+st);
- printBitSet(bs);
- intit=rand.nextInt();
- BitSetbi=newBitSet();
- for(inti=31;i>=0;i--)
- if(((1<<i)&it)!=0)
- bi.set(i);
- else
- bi.clear(i);
- System.out.println("intvalue:"+it);
- printBitSet(bi);
- //Testbitsets>=64bits:
- BitSetb127=newBitSet();
- b127.set(127);
- System.out.println("setbit127:"+b127);
- BitSetb255=newBitSet(65);
- b255.set(255);
- System.out.println("setbit255:"+b255);
- BitSetb1023=newBitSet(512);
- //Withoutthefollowing,anexceptionisthrown
- //intheJava1.0implementationofBitSet:
- //b1023.set(1023);
- b1023.set(1024);
- System.out.println("setbit1023:"+b1023);
- }
- staticvoidprintBitSet(BitSetb){
- System.out.println("bits:"+b);
- Stringbbits=newString();
- for(intj=0;j<b.size();j++)
- bbits+=(b.get(j)?"1":"0");
- System.out.println("bitpattern:"+bbits);
- }
- }///:~
欢迎光临 仓酷云 (http://ckuyun.com/) | Powered by Discuz! X3.2 |