tags:

views:

212

answers:

6

how do i define 24 bit array in c++? (variable declaration) thank you

+2  A: 

An unsigned byte array of 3 bytes is 24 bits. Depending on how you are planning to use it, it could do.

unsigned char arrayname[3]

As @GMan points out you should be aware that it's not 100% of all systems that has 8 bits chars.

Jonas Elfström
A byte (`char`) is not necessarily 8 bits.
GMan
@GMan, That is incorrect, according to the C standard. I'll have to look at the C++ standard.
strager
@strager: A `char` is not necessarily 8 bits in both C and C++. It is necessarily *at least* 8 bits, but is not necessarily *exactly* 8 bits.
GMan
According to 5.3.3, `sizeof(char)==1` always. However, it defines that the number of bits in a byte is implementation defined. So I guess I was wrong.
strager
@strager: sizeof(char) = 1 byte, not one *octet*. :)
Michael Foukarakis
No, it is actually correct. The C standard defines the CHAR_BIT macro and mandates a minimum size of 8, but also allows for larger sizes5.2.4.2.1 Sizes of integer types <limits.h>"Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign."
Jim Brissom
I must have misread the C standard somewhere, too, perhaps mistaking the `#define CHAR_BIT 8` example as 'law'.
strager
But ... who cares. I use `uint8_t` anyway. ;P It's portable across GCC and VC++ anyway.
strager
@stager: And in C99, given `stdint.h` it is standard.
GMan
@GMan, Yup, and Boost has it for C++ if necessary (though it's not as convenient due to silly namespaces).
strager
@strager: You can also make your own `stdint.h` that includes Boost's then has a series of using-directives. :)
GMan
+12  A: 

There is no 24-bit variable type in C++.

You can use a bitpacked struct:

struct ThreeBytes {
    uint32_t value:24;
};

But it is not guaranteed that sizeof ThreeBytes == 3.

You can also just use uint32_t or sint32_t, depending on what you need.

Another choice is to use std::bitset:

typedef std::bitset<24> ThreeBytes;

Then make an array out of that:

ThreeBytes *myArray = new ThreeBytes[10];

Of course, if you really just need "three bytes", you can make an array of arrays:

typedef uint8_t ThreeBytes[3];

Note that uint8_t and friends are non-standard, and are used simply for clarification.

strager
+2  A: 

If you intend to perform bitwise operations on them, then simply use an integral type that has at least 24 bits. An int is 32 bits on most platforms, so an int may be suitable for this purpose.

EDIT: Since you actually wanted an array of 24 bit variables, the most straightforward way to do this is to create an array of ints or longs (as long as it's an integral data type that contains at least 24 bits) and treat each element as though it was 24 bits.

In silico
A: 

Depending on your purpose (for example, if you are concerned that using a 32bit type might waste too much memory), you might also consider creating an array of bytes with three times the length.

I used to do that a lot for storing RGB images. To access the n'th element, you would have to multiply by three and then add zero, one or two depending on which "channel" out of the element you wanted. Of course, if you want to access all 24 bits as one integer, this approach requires some additional arithmetics.

So simply unsigned char myArray[ELEMENTS * 3]; where ELEMENTS is the number of 24bit elements that you want.

Siberion
Reading the comments to the answer above, my approach is obviously also flawed in the way that a char is not necessarily 8 bits.. but yeah.
Siberion
A: 

Use bitset or bitvector if they are supported on your platform. (They are :) )

Basilevs
A: 
std::vector<std::bitset<24> > myArray;
MattyT