MCPcopy Index your code
hub / github.com/BruceEckel/OnJava8-Examples / main

Method main

collectiontopics/Bits.java:16–60  ·  view source on GitHub ↗
(String[] args)

Source from the content-addressed store, hash-verified

14 System.out.println("bit pattern: " + bbits);
15 }
16 public static void main(String[] args) {
17 Random rand = new Random(47);
18 // Take the LSB of nextInt():
19 byte bt = (byte)rand.nextInt();
20 BitSet bb = new BitSet();
21 for(int i = 7; i >= 0; i--)
22 if(((1 << i) & bt) != 0)
23 bb.set(i);
24 else
25 bb.clear(i);
26 System.out.println("byte value: " + bt);
27 printBitSet(bb);
28
29 short st = (short)rand.nextInt();
30 BitSet bs = new BitSet();
31 for(int i = 15; i >= 0; i--)
32 if(((1 << i) & st) != 0)
33 bs.set(i);
34 else
35 bs.clear(i);
36 System.out.println("short value: " + st);
37 printBitSet(bs);
38
39 int it = rand.nextInt();
40 BitSet bi = new BitSet();
41 for(int i = 31; i >= 0; i--)
42 if(((1 << i) & it) != 0)
43 bi.set(i);
44 else
45 bi.clear(i);
46 System.out.println("int value: " + it);
47 printBitSet(bi);
48
49 // Test bitsets >= 64 bits:
50 BitSet b127 = new BitSet();
51 b127.set(127);
52 System.out.println("set bit 127: " + b127);
53 BitSet b255 = new BitSet(65);
54 b255.set(255);
55 System.out.println("set bit 255: " + b255);
56 BitSet b1023 = new BitSet(512);
57 b1023.set(1023);
58 b1023.set(1024);
59 System.out.println("set bit 1023: " + b1023);
60 }
61}
62/* Output:
63byte value: -107

Callers

nothing calls this directly

Calls 2

printBitSetMethod · 0.95
setMethod · 0.65

Tested by

no test coverage detected