Would a boolean array of size 32 take more space than an integer variable, for example? If so, then why and by how much?
CLARIFICATION:
In java (if that is relevant, forgive me - I am not sure). Would this line:
boolean arr=new boolean[32];
take more space than this line:
int num;
An array of 32 boolean
s in Java takes about eight times the space of a Java int
. This is because in most computer architectures the smallest addressable unit of memory is an eight-bit byte, so making an array of "packed" booleans requires additional overhead.
If you would like to use one bit per boolean, use BitSet
class instead of an array of booleans. Note that you would get some overhead in addition to the data itself, so using such data structures for only 32 bits may not be economical enough to justify switching away from a simple array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With