HomeSearch

Java BitSet Examples

These Java examples focus on the BitSet collection, a dynamically-resizing array of bits. It efficiently holds true and false values.
BitSet. Bits are efficient. Imagine we need to store many true or false values. Using bits, not bytes, saves 7 bits per value. This adds up.
Java is not slow. But some programs written in it are—with a collection like BitSet, we can improve performance in certain programs. Sometimes the results are dramatic.
Set, get example. We begin with a simple example of BitSet: we set two bits, at positions 10 and 100. Then we get some bits. Position 5 is false, but 10 and 100 are true.

Set: This sets a bit to true (or 1). To set a bit to zero, please use the clear method or specify false in set.

Get: This returns a boolean indicating the value of the bit. True is 1 and false is 0.

Java program that creates, sets BitSet import java.util.BitSet; public class Program { public static void main(String[] args) { // Set two bits in a BitSet. BitSet b = new BitSet(); b.set(10); b.set(100); // Get values of these bit positions. boolean bit1 = b.get(5); boolean bit2 = b.get(10); boolean bit3 = b.get(100); System.out.println(bit1); System.out.println(bit2); System.out.println(bit3); } } Output false true true
Flip. This method makes some programs simpler. It changes the value of a 0 bit to 1, and the value of a 1 bit to zero. By default in BitSet, a bit is zero.
Java program that calls flip import java.util.BitSet; public class Program { public static void main(String[] args) { BitSet b = new BitSet(); // Set this bit. b.set(3); System.out.println(b.get(3)); // Flip the bit. b.flip(3); System.out.println(b.get(3)); } } Output true false
Set range. Bits can be set in a range. In this way we can avoid writing excess loops to set bits. Here we set the bits at indexes 2, 3 and 4 to true. We display them in a for-loop.For
Java program that sets range import java.util.BitSet; public class Program { public static void main(String[] args) { BitSet b = new BitSet(); // Set bits in this range to true. b.set(2, 5); // Display first five bits. for (int i = 0; i < 5; i++) { System.out.println(b.get(i)); } } } Output false false true true true
ToByteArray. A BitSet is composed of bytes. With toByteArray we can access the backing store, a byte array. Each byte has a value determined by the bits that are set.Arrays

Here: We set the first bit of the first byte to 1, so the first byte equals 1. The second byte, at position 8, also equals 1.

Java program that uses toByteArray import java.util.BitSet; public class Program { public static void main(String[] args) { BitSet b = new BitSet(); // Set bit 0 and bit 8 to true. b.set(0); b.set(8); // Convert to byte array. for (byte value : b.toByteArray()) { System.out.println(value); } } } Output 1 1
Get range. With get, we usually access a single bit. But we can get a range of bits, like a substring, as a completely new BitSet instance. Note how the positions of the set bits change.
Java program that gets range import java.util.BitSet; public class Program { public static void main(String[] args) { BitSet b = new BitSet(); b.set(3); b.set(5); // Get range of bits from original set. BitSet b2 = b.get(3, 6); // Display first five bits. for (int i = 0; i < 5; i++) { System.out.println(b2.get(i)); } } } Output true false true false false
Clear. This method sets a bit to zero. It is the same thing as passing a false value as the second argument to the set method. We also can clear a range of bits.

No arguments: With no arguments, clear() will erase all bits in the BitSet. This is similar to creating an entirely new instance.

Java program that uses clear import java.util.BitSet; public class Program { public static void main(String[] args) { BitSet b = new BitSet(); // Set first four bits to true. b.set(0, 4); // Clear first two bits. b.clear(0, 2); // Display first four bits. System.out.println(b.get(0)); System.out.println(b.get(1)); System.out.println(b.get(2)); System.out.println(b.get(3)); } } Output false false true true
BitSet is a space optimization. With less memory usage, programs often are faster too. This collection introduces some complexity, but is simpler than directly manipulating bits.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls