tags:

views:

161

answers:

3

I need to store a 30 letter combination, but each letter can only be "0", "1" or "2". When I use sizeof(myString), it returns 32.

I want to use this 30 letter combination to access a row of an array, so I'm wondering if it is possible to use a 3 value bool of some sort to store 1 of 3 values in.

+12  A: 

3^30 = 205891132094649 (~2E14), which is less than the maximum value of a 64-bit integer (~2E19), so you could map the strings to 64-bit ints in a 1:1 fashion.

An obvious way to do this would be to treat your string as a base-3 number, which would be quite slow to convert. Much faster would be to treat it as base 4, then conversion can be done entirely with bit shifts (no modulus division / multiplication), this is possible since 4^30 is still less than 2^64.

Autopulated
Thank you, this would probably work!
noryb009
+2  A: 

The smallest unit of size C and C++ let you deal with (without bitfields in structures that would make your code very impractical) is the char. Even bool resolves to the size of a char even though it uses only a single bit. Therefore, you won't make any memory gain from using another type. The only possible improvement would be to use a type completely different from an array.

zneak
`sizeof(bool)` is implementation defined.
Dennis Zickefoose
@Dennis Zickefoose: but it's certainly not going to be less than 1.
zneak
But it could be larger. It doesn't "resolve" to `char` or `unsigned char`. It is just `bool`.
Dennis Zickefoose
@Dennis Zickefoose: I understand your point, but you missed mine: `char` is the smallest unit of size a single variable can occupy, and even `bool` won't go lower than that. Please stop being pedantic for the sake of being pedantic. Comparing with `char` is relevant because while the size in bits of `char` is implementation-defined, `sizeof(char)` is bound by the standard to be 1, and _nothing_ can be allocated smaller than that size.
zneak
If you don't want people to be pedantic, then don't be wrong.
Dennis Zickefoose
@Dennis Zickefoose: I'll be waiting for the day `sizeof(bool)` returns 2.
zneak
@zneak: I've seen systems where `sizeof(char*)` is different to `sizeof(void*)`, so a `bool` being the size of a machine word would not be at all odd. Might be wasteful in an array of the things though. :-)
Donal Fellows
@Donal Fellows: pointers not being the same size sounds extremely mysterious to me. That being said, I should stop arguing `sizeof(bool)` shouldn't be greater than `sizeof(char)` because my point was that `sizeof(bool)` can't be _lower_ than `sizeof(char)`.
zneak
@zneak: It wasn't a classical architecture. The real smallest addressable unit was a machine word (which I think was 32-bits on that system; might've been larger) and addressing of characters was done by applying bit splicing to words; `char*` was larger because it was actually a `struct` (!) containing the address of the word and the offset within the word. We tried to support that silly architecture (some sort of Cray IIRC) for a few years, and then decided that life was to short for such madness; even real-mode x86 was nicer to work with (provided you could choose the memory model).
Donal Fellows
A: 

Boost has a tri-bool library for 3-valued booleans, but I'm not actually recommending you use it here. You're better off just mapping the values into 2-bit numbers packed into 64 bits, as mentioned above.

swestrup