简体   繁体   中英

Convert a String to array of bits

I would like to convert a String consisting of 0's and 1's to an array of bits.
The String is of length ~30000 and is sparse (mostly 0s, few 1s)
For example, given a string
"00000000100000000010000100000000001000"
I would like to convert it to an array of bits which will store
[00000000100000000010000100000000001000]

I am thinking of using BitSet or OpenBitSet Is there a better way? The use case is to perform logical OR efficiently.

I am thinking along these lines

final OpenBitSet logicalOrResult = new OpenBitSet(); 
for (final String line : lines) {

   final OpenBitSet myBitArray = new OpenBitSet(); 
   int pos = 0;
   for (final char c : str.toCharArray()) {
         myBitArray.set(pos) = c;
         pos++;
   }
   logicalOrResult.or(myBitArray);
}

BigInteger can parse it and store it, and do bitwise operations:

BigInteger x = new BigInteger(bitString, 2);
BigInteger y = new BigInteger(otherBitString, 2);
x = x.or(y);
System.out.println(x.toString(2));

A BitSet ranging over values between 0 and 30000 requires a long array of size less than 500, so you can assume that BitSet.or (or the respective OpenBitSet method) will be sufficiently fast, despite the sparsity. It looks like OpenBitSet has better performance than BitSet , but apart from this it doesn't really matter which you use, both will implement or efficiently. However, be sure to pass the length of the String to the (Open)BitSet constructor to avoid reallocations of the internal long array during construction!

If your strings are much longer and your sparsity is extreme, you could also consider storing them as a sorted list of Integer s (or int s, if you use a library like Trove), representing the indices which contain a 1 . A bitwise or can be implemented in a merge(sort)-like fashion, which is quite efficient (time O(n + m), where n, m are the numbers of ones in each string). I suspect that in your scenario it will be slower than the BitSet approach though.

You can iterate through each character:

boolean[] bits = new boolean[str.length];

for (int i=0;i<str.length;i++) {
    if (str.charAt(i).equals("1")
        bits[i] = true;
    else if (str.charAt(i).equals("0")
        bits[i] = false;
}

If you want to be memory efficient, you could try RLE (Run Length Encoding) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM