2

I just found out that there is BitSet in java. There are already arrays and similar data structures. Where can BitSet be used?

  • 1
    Anywhere `BitSet` is defined. OT: Tons of comparisons online, such as [boolean array vs BitSet](https://stackoverflow.com/questions/605226/boolean-vs-bitset-which-is-more-efficient). "*There are already arrays and similar data structures*" - Why have `boolean` when we can represent `true/false` using `int` values? – Vince Jun 10 '17 at 19:27
  • A `BitSet` is a very efficient for a set of non-negative integers within a (not too large) range. Much more efficient than arrays and hash maps. An `EnumSet` is implemented the same way as a `BitSet`. – Ole V.V. Jun 10 '17 at 19:31
  • The link escapes me, but a couple of times someone has asked how to determine whether two strings contain the same characters. One good answer for very long strings is build a bit set of the characters of each string and compare the bit sets. – Ole V.V. Jun 10 '17 at 19:34
  • Do search Stack Overflow and other places for examples. – Ole V.V. Jun 10 '17 at 19:34

3 Answers3

4

As the above answer only explains what a BitSet is, I am providing here an answer of how I use BitSet and why. At first, I did not knew that the BitSet construct exists. I have a QR Code generator in C++ and for flexible reasons I don't want to use a specific Bitmap structures in returning this QR Code back to the caller. The QR Code is just black and white and can be represented as a series of bits. The problem was that in the JNI C++, I have to return the byte array that represents these series of bits and then I have to return the count of bits. Note that the size of the bytes array alone could not tell the count of bits. In effect, I am face with a scenario wherein my JNI C++ has to return two values:

  • the byte[] array
  • the count of bits

My first solution, was to return an array of boolean. The content of this array are the QR Code pixels, and the square root of the length of the array is the length of the side. Of course this worked but I felt wasted because it is supposed to be a series of bits. My next attempt was to return Pair<int, byte[]> object which, after lots of hair pulling i am not able to make it work in C++. Here comes the BitSet(145) construct. By returning this BitSet object, I am conveying two types of information i listed above. But there is minor trick. If QR Code pixel has total 144 pixels, because one side is 12, then you have to allocate BitSet(145) and do obj.set(144). That is, we introduce an artificial last bit that we then set, but this last bit is not part of the QR Code pixels. This ensures that, BitSet::length() correctly returns the bit count. So in Kotlin:

var pixels:BitSet = getqrpixels(inputdata)
var pixels_len = pixels.length() - 1
var side = sqrt(pixels_len.toFloat()).toInt()
drawSquareBitmap(pixels, side)

And thus, is my unexpected use case of this mysterious BitSet.

daparic
  • 3,794
  • 2
  • 36
  • 38
2

Take a look at this:

https://docs.oracle.com/javase/7/docs/api/java/util/BitSet.html

A BitSet is a vector of bits. Each entry in the list is either true (1) or false (0). The BitSet class comes with methods that resemble the bitwise operators. It is a little bit more flexible then a normal binary type.

-1

BitSet, unlike a boolean[], is actually a dynamically sized bitmask. Essentially, instead of using booleans to store values, it uses longs, where each of the longs 64 bits are used to store a single bit.

itzjackyscode
  • 970
  • 9
  • 27