array of 1 byte

I have some information that i need to store. 12 x 4 bits. It is a application where every bit counts so i want to store the information in 6 bytes. But now my question, Is it possible to make an array containing 12x4 bits, so the information is easy accessible?

i saw this:
1
2
3
4
5
6
7
typedef struct 
{
	unsigned char A: 4;
	unsigned char B: 4;
	unsigned char C: 4;
        etc...
}data;


But is it possible to make an array with 4 bits for each element?
Something like this:

1
2
3
4
typedef struct
{
	unsigned char (val:4)[12];/* not valid */
}data;


does something exsist like this?
I think redefining structure format will not go well with the C++ libraries that use struct, so it must be invalid..
Here is when I want for bitset to provide iterators, or access like valarray with slide

If you are going to make it 4 bits always, then you could specialize a class to handle it, providing setters and getters

Like (not tested)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
template <size_t N>
class nibble_array{
public:
  typedef unsigned char byte;
  byte get(size_t index){
    byte mask = (index%2==0)? 0x0f: 0xf0;
    byte value = data[index/2] bitand mask;
    if(index%2) value >>=4;
    return value;
  }
  void set(size_t index, byte value){
    if(index%2) value <<= 4;
    byte mask = (index%2==0)? 0x0f: 0xf0;
    data[index/2] and_eq compl mask; //reset nibble
    data[index/2] or_eq value; 
  }

private:
  byte data [N/2 + N%2];
};
Last edited on
Why not use a multi-dimensional array?
@Owain example?
1
2
3
4
typedef struct
{
	unsigned char [12][4];
}data;



Then you sort of have a table of data:

x 0 1 2 3 4
0 _ _ _ _ _
1 _ _ _ _ _
2 _ _ _ _ _
3 _ _ _ _ _
4 _ _ _ _ _
...
Last edited on
closed account (zb0S216C)
You can't create an array of n bits, only 8 bits (1 byte) because most machines cannot address a bit directly. On most machines, bits don't have an address. Since a nybble is half a byte (usually 4 bits), it's too small to be granted an address. Therefore, it cannot be an array.

What you'll have to do is use 1 byte to store 2 nybbles. This is achieved either by manually accessing each individually bit (tedious), or use bit-masking. For example:

The tedious method:
1
2
3
4
5
6
7
8
9
10
int main()
{
    signed char Bytes[2] = {0}; // 4 nybble maximum.

    // Access the first nybble's bits (this sounds wrong):
    Bytes[0] |= (1 << 0);
    Bytes[0] |= (1 << 1);
    Bytes[0] |= (1 << 2);
    Bytes[0] |= (1 << 3);
}

The bit-masking method:
1
2
3
4
5
6
7
8
9
10
11
12
13
namespace Globals
{
    const signed char FIRST_NYBBLE((1 << 0) | (1 << 1) | (1 << 2) | (1 << 3));
    const signed char SECOND_NYBBLE((1 << 4) | (1 << 5) | (1 << 6) | (1 << 7));
}

int main()
{
    signed char Bytes[2] = {0};

    // Access:
    Bytes[0] |= Globals::FIRST_NYBBLE;
}

These are examples, of course.

Wazzak
^ that is not access, but setting. (turn on, if you prefer)
I thinkk that it would be better to use std::bitset<4>

std::bitset<4> Data[12];
Sadly, no
OP wrote:
It is a application where every bit counts so i want to store the information in 6 bytes
How about this? :)

Is it possible to make an array containing 12x4 bits, so the information is easy accessible?
Topic archived. No new replies allowed.