1

I am writing a program which necessitates speed and a low memory overhead. In it, I have to make a 2d array of booleans, which will likely get very big.

In Java, I know that a boolean is an "overlay" to a char, so it takes up one byte.

Is there any way to make an array of booleans, each of which are only 1 bit, the storage space technically necessary. Thanks!

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Justin Sanders
  • 313
  • 2
  • 12
  • 8
    Use [`BitSet`](https://docs.oracle.com/javase/8/docs/api/java/util/BitSet.html). – shmosel Mar 16 '17 at 22:18
  • He is concerned about optimization, won't BitSet be a bit much? – Thomas Mar 16 '17 at 22:19
  • Start by thinking of a `char` as an array of 8 booleans. And go from there. – Joe C Mar 16 '17 at 22:19
  • Joe C, yeah depending on how many options he needs, he just needs to do OR and AND (or NOT). – Thomas Mar 16 '17 at 22:22
  • 2
    See: [`boolean[]` vs. BitSet: Which is more efficient?](http://stackoverflow.com/q/605226/5221149) – Andreas Mar 16 '17 at 22:34
  • @JoeC, a `char` in Java has 16 bits, not 8. – Louis Wasserman Mar 16 '17 at 23:01
  • 1
    Bit manipulation as a rule is slower than byte manipulation. So you have to choose between "speed" (whatever you mean by that) and "low" memory overhead (whatever you mean by that), but in this case you probably can't have both. And you should tell us how big "very big" is. "Very" is not an engineering concept. Put a number on it. – Lew Bloch Mar 16 '17 at 23:11
  • 1
    `I am writing a program which necessitates speed and a low memory overhead.` I wonder what made you choose Java then. :) – biziclop Mar 16 '17 at 23:48
  • Definitely `BitSet`. If it's two dimensional, a `BitSet[]` or a `List`. – Louis Wasserman Mar 16 '17 at 23:50

2 Answers2

1

What you're looking for is a bit field. Java does not support bit fields. Basically with C/C++ the compiler adds in the bit handling (1 << 2, 1<<3, etc) for you.

Good workaround: Implementing a bitfield using java enums

Community
  • 1
  • 1
Thomas
  • 1,401
  • 8
  • 12
  • Yeah, [`java.util.BitSet`](https://docs.oracle.com/javase/8/docs/api/java/util/BitSet.html) comes with Java, does the bit handling for you, takes about one bit of space per `boolean`, and works nicely. – Chai T. Rex Mar 17 '17 at 00:10
1

As it was already mentioned, you can use primitives as an array of bits. That is not straight forward to work with, but at least it reduces the amount of data that you store.

With the magic of method convertToBinary that I found here: Integer to binary array We can work with such numbers easily.

e.g.

 public static void main(String[] args) {
    Integer[] flags = {
            0b0101_0101_0101_0101_0101_0101_0101_0101,
            0b0101_0101_0101_0101_0101_0101_0101_0101,
            0b0101_0101_0101_0101_0101_0101_0101_0101
    };
    for (Integer flag : flags) {
        System.out.println(Arrays.toString(getBooleanArray(flag)));
    }
}

private static boolean[] getBooleanArray(Integer value) {
    String valueAsString = Integer.toBinaryString(value).replaceFirst("^0+(?!$)", "");
    return convertToBinary(value, valueAsString.length());
}

private static boolean[] convertToBinary(int b, int size) {
    boolean[] binArray = new boolean[size];
    boolean bin;
    for (int i = size - 1; i >= 0; i--) {
        if (b % 2 == 1) bin = true;
        else bin = false;
        binArray[i] = bin;
        b /= 2;
    }
    return binArray;
}

Output:

[true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true]

[true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true] [true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true]

Hope that helps.

Community
  • 1
  • 1
dvelopp
  • 4,095
  • 3
  • 31
  • 57