80 lines
2.2 KiB
Java
80 lines
2.2 KiB
Java
// understandingcollections/Bits.java
|
|
// (c)2016 MindView LLC: see Copyright.txt
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
|
// Demonstration of BitSet
|
|
import java.util.*;
|
|
|
|
public class Bits {
|
|
public static void printBitSet(BitSet b) {
|
|
System.out.println("bits: " + b);
|
|
StringBuilder bbits = new StringBuilder();
|
|
for(int j = 0; j < b.size() ; j++)
|
|
bbits.append(b.get(j) ? "1" : "0");
|
|
System.out.println("bit pattern: " + bbits);
|
|
}
|
|
public static void main(String[] args) {
|
|
SplittableRandom rand = new SplittableRandom(47);
|
|
// Take the LSB of nextInt():
|
|
byte bt = (byte)rand.nextInt();
|
|
BitSet bb = new BitSet();
|
|
for(int i = 7; i >= 0; i--)
|
|
if(((1 << i) & bt) != 0)
|
|
bb.set(i);
|
|
else
|
|
bb.clear(i);
|
|
System.out.println("byte value: " + bt);
|
|
printBitSet(bb);
|
|
|
|
short st = (short)rand.nextInt();
|
|
BitSet bs = new BitSet();
|
|
for(int i = 15; i >= 0; i--)
|
|
if(((1 << i) & st) != 0)
|
|
bs.set(i);
|
|
else
|
|
bs.clear(i);
|
|
System.out.println("short value: " + st);
|
|
printBitSet(bs);
|
|
|
|
int it = rand.nextInt();
|
|
BitSet bi = new BitSet();
|
|
for(int i = 31; i >= 0; i--)
|
|
if(((1 << i) & it) != 0)
|
|
bi.set(i);
|
|
else
|
|
bi.clear(i);
|
|
System.out.println("int value: " + it);
|
|
printBitSet(bi);
|
|
|
|
// Test bitsets >= 64 bits:
|
|
BitSet b127 = new BitSet();
|
|
b127.set(127);
|
|
System.out.println("set bit 127: " + b127);
|
|
BitSet b255 = new BitSet(65);
|
|
b255.set(255);
|
|
System.out.println("set bit 255: " + b255);
|
|
BitSet b1023 = new BitSet(512);
|
|
b1023.set(1023);
|
|
b1023.set(1024);
|
|
System.out.println("set bit 1023: " + b1023);
|
|
}
|
|
}
|
|
/* Output:
|
|
byte value: 118
|
|
bits: {1, 2, 4, 5, 6}
|
|
bit pattern: 0110111000000000000000000000000000000000000000
|
|
000000000000000000
|
|
short value: 9795
|
|
bits: {0, 1, 6, 9, 10, 13}
|
|
bit pattern: 1100001001100100000000000000000000000000000000
|
|
000000000000000000
|
|
int value: -645079414
|
|
bits: {1, 3, 7, 9, 10, 11, 12, 14, 15, 18, 19, 23, 24, 27,
|
|
28, 30, 31}
|
|
bit pattern: 0101000101111011001100011001101100000000000000
|
|
000000000000000000
|
|
set bit 127: {127}
|
|
set bit 255: {255}
|
|
set bit 1023: {1023, 1024}
|
|
*/
|