# bitfield "array"

• 04-11-2006
bibsik
bitfield "array"
how do i make a bitfield array when in theory it is not supposed to exist because pointers just don't work with bitfields
i know i can try with a struct but can i create a dynamic packed array?

• 04-11-2006
Sebastiani
>> how do i make a bitfield array

you can't, but an std::bitset is a good substitute.
• 04-11-2006
Salem
Eg, you have this private in your class
unsigned long bitArray[10];

You overload the [ ] operator for the class to do what you want.
- extract an array index (i / 32) and a bit position (i % 32)
- use bitwise operators &,| to extract or insert a bit.
• 04-12-2006
jafet
Sounds a little tricky there Salem... IMHO I wouldn't mind wasting a little space putting a bit in a char for easy access.

By the way, I've always been wondering... how much space does a bool take up? :)
• 04-12-2006
Sebastiani
>> By the way, I've always been wondering... how much space does a bool take up?

a byte

>> Sounds a little tricky

crude example:

Code:

```struct bits {         protected:                 struct proxy         {                 proxy(unsigned char * ptr, unsigned char index)                 : base(ptr), mask(1 << index)                 {                                         }                                 operator bool (void)                 {                         return *base & mask;                 }                                 proxy &                 operator = (bool state)                 {                         *base = state ? *base | mask : *base & ~mask;                         return *this;                 }                                 unsigned char * base;                 unsigned char mask;         };                 public:                 bits(void)         : base(0), limit(0)         {                         }                 template <class T>         bits(T & obj)         {                 set(obj);         }                        bits(void * ptr, unsigned len)         {                 set(ptr, len);         }                 bits(void * ptr, void * end)         {                 set(ptr, end);         }                        template <class T>         bits &         set(T & obj)         {                 return set(&obj, sizeof(obj));         }                        bits &         set(void * ptr, unsigned len)         {                 base = (unsigned char*)ptr;                 limit = base + len;                 return *this;         }                 bits &         set(void * ptr, void * end)         {                 base = (unsigned char*)ptr;                 limit = (unsigned char*)end;                 return *this;         }                 proxy         operator [] (unsigned index)         {                 return proxy(&base[index / 8], index % 8);         }         protected:                 unsigned char * base, * limit;        }; int main() {         int i = 16;         bits ib(i);         cout << i << endl;         ib[1] = 1;         cout << i << endl; }```
• 04-12-2006
DougDbug
FYI bibsik,

I don't know about your application, but in the hardware world we work with individual bits a lot. The trick is to use hexadecimal instead of binary. With a little study and practice, you can learn to convert numbers of any size between hex and binary. (...Not so easy with decimal!)

- In C++, hex is much easier than binary. Hex is built into cin and cout, and you can use it directly in your source code.

- It's also easier to read, write, and speak... It's a lot easier to say "F5" than "1111 0101". When you get more than 8 bits, binary starts getting really tough.

Quote:

how do i make a bitfield array when in theory it is not supposed to exist because pointers just don't work with bitfields
i know i can try with a struct but can i create a dynamic packed array?
I'm not sure what you are trying to do... In the computer's memory, an integer is stored in binary.* So, with a 16-bit integer, you can store the "binary state" of 16 different "things". Back in my hardware world... one bit might represent the on-off state of an LED. Another bit might represent the on-off state of a particular switch. If you display that integer in hex or binary, you can easily "see" the state of each "thing". (You can do the same thing if you display the number in decimal, it just takes a little more work to figure-out the individual bit-states.)

* Of course, everything is stored in binary. The application (and operating system) have to keep track of what the "numbers" at at each memory address are used for. The binary number might simply represent the numeric integer-value (1111 0101 = 245 decimal), or it might represent an ASCII character, or something else... like a machine-language CPU instruction.
• 04-13-2006
jafet
What I meant by "tricky": several computer instructions just to extract one bit. Of course, I could be wrong about that.

Doug has a good point there :)