# 3D Array of Bool

Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last
• 06-26-2007
cybernike
3D Array of Bool
I program with the g++ compiler on a linux workstation. I am testing a very simple program that has only a 1000x1000x1000 array of boolean variables. I thought each boolean variable occupied only one bit. So the program would require appromixately 10^9 bits ~= 119 megabytes of RAM. However, the system monitor tells me that the program requires about 976 megabytes of RAM.

Why?

Thanks.
• 06-26-2007
indigo0086
a bool is a byte in size, generally.
• 06-26-2007
cybernike
O..that explains why. But why would it use 1 byte if 1 bit can already represent 0 or 1. Anyway, is there any data type that uses only 1 bit? For what I am doing, even a 1000x1000x1000 array would not be enough.

Thanks.
• 06-26-2007
Queatrix
>> But why would it use 1 byte if 1 bit can already represent 0 or 1.
Because bool doesn't just hold true or false, it also hold integers.

>> For what I am doing, even a 1000x1000x1000 array would not be enough.
If you're worried about room, make a matrix of chars instead, and alter each bit (0x80 for starters) to hold your "bool" value; so that way one char holds 8 "varibles".
• 06-26-2007
indigo0086
Quote:

Originally Posted by cybernike
O..that explains why. But why would it use 1 byte if 1 bit can already represent 0 or 1. Anyway, is there any data type that uses only 1 bit? For what I am doing, even a 1000x1000x1000 array would not be enough.

Thanks.

The way memory is structured it would probably be wasteful and time consuming traversing the whole of memory by bits.
• 06-26-2007
cybernike
>>If you're worried about room, make a matrix of chars instead, and alter each >>bit (0x80 for starters) to hold your "bool" value; so that way one char holds 8>> "varibles".

Would you please elaborate more? or give me a reference? It seems to be a good solution for my problem.

Thanks.
• 06-26-2007
h_howee
Quote:

is there any data type that uses only 1 bit?
bitsets

>>Would you please elaborate more? or give me a reference? It seems to be a good solution for my problem.

a char can hold 8bits worth of data. you can use each bit as a seperate variable, for example:
if you want to store the values true in the first bit of a char...
Code:

```char a; a |= true*2^0; //the zero is like the array index (if you were using an array) a |= true*2^1; //stores true in the second bit // and so on... //i think this is how a bit goes from true to false if (a & 2^0) //checks if the first bit is true     a ^= true*2^0; //if so, xor it with true to change it to false```
the 2^x in bold means 2 to the power of x, not xor. i think having it written this way makes the (pseudo)code slightly clearer
• 06-26-2007
ChaosEngine
Quote:

Originally Posted by Queatrix
>> But why would it use 1 byte if 1 bit can already represent 0 or 1.
Because bool doesn't just hold true or false, it also hold integers.

what the hell are you talking about?
• 06-27-2007
cybernike
Quote:

Originally Posted by h_howee
bitsets

>>Would you please elaborate more? or give me a reference? It seems to be a good solution for my problem.

a char can hold 8bits worth of data. you can use each bit as a seperate variable, for example:
if you want to store the values true in the first bit of a char...
Code:

```char a; a |= true*2^0; //the zero is like the array index (if you were using an array) a |= true*2^1; //stores true in the second bit // and so on... //i think this is how a bit goes from true to false if (a & 2^0) //checks if the first bit is true     a ^= true*2^0; //if so, xor it with true to change it to false```
the 2^x in bold means 2 to the power of x, not xor. i think having it written this way makes the (pseudo)code slightly clearer

Thanks, the syntax is probably too cumbersome for my problem. You once mentioned bitsets and I played with it. However, both bitset<1> and bitset<8> use 4 bytes after I checked with the function sizeof( ). Why?
• 06-27-2007
Salem
> both bitset<1> and bitset<8> use 4 bytes after I checked with the function sizeof( ). Why?
They're still going to use the smallest amount of allocatable storage, no matter how few bits you choose.
Try something like say 1000 or 2000, then you should see the benefit if it really is using just bits internally.
• 06-27-2007
robatino
I'm surprised that no one mentioned vector<bool>, which unlike bitset<> can be used if the size is determined at runtime. It has issues, but is worth knowing about, since even if it's removed from a future version of the standard, the functionality will probably be duplicated somehow.

http://www.informit.com/guides/conte...seqNum=98&rl=1
• 06-27-2007
iMalc
Yeah guys, why no vector<bool> mentions?
Or in this case:
Code:

`vector< vector< vector<bool> > >`
Yes I know that the explicit specialisation is a hack that doesn't work exactly the same as every other data type, but for most purposes it works fine.
I'm sure boost has something better though.

I presume this is for voxel data? Have you considered compressing it? (beyond using 1-bit per voxel of course)
I mean you could probably use something like RLE to compress it in such a way that you can still quickly determine the status of an individual voxel. Reducing the memory usage by even say a factor of four would probably still provide a speed increase after decompression, as memory would probably be your bottleneck. Of course it depends on how often you write to it. If it is read in and then not changed, doing this should be sweet as!
• 06-27-2007
indigo0086
I'd say if he wants to test each bit then he should use a vector of bitsets. That way each element can be however wide he would like and he can easily access, test, and set each bit.
• 06-27-2007
KIBO
Why do you need 10^9 bools anyway?
• 06-27-2007
cybernike
Quote:

Originally Posted by iMalc
Yeah guys, why no vector<bool> mentions?
Or in this case:
Code:

`vector< vector< vector<bool> > >`
Yes I know that the explicit specialisation is a hack that doesn't work exactly the same as every other data type, but for most purposes it works fine.
I'm sure boost has something better though.

I presume this is for voxel data? Have you considered compressing it? (beyond using 1-bit per voxel of course)
I mean you could probably use something like RLE to compress it in such a way that you can still quickly determine the status of an individual voxel. Reducing the memory usage by even say a factor of four would probably still provide a speed increase after decompression, as memory would probably be your bottleneck. Of course it depends on how often you write to it. If it is read in and then not changed, doing this should be sweet as!

You are right that it is for voxel data (I had to look up what voxel means). I am writing a Monte Carlo program that describes brownian motions.
I will definitely look into RLE, if it could reduce memory usage and still provide a speed increase. However, I always thought a compression would usually slow things down? not true? Also, I need to constantly change the true/false values in the array, since the particles move.
Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last