if data is always between 0 and 7, can it be stored in 4 bits?

B

bluekite2000

int a[4]={1,2,3,4};
but i d rather not use int (which on my machine is 4 bytes) I d like to
store all 4 values in 4elements*4bits=16bits=2bytes.
How do i do that?
 
V

Victor Bazarov

int a[4]={1,2,3,4};
but i d rather not use int (which on my machine is 4 bytes) I d like to
store all 4 values in 4elements*4bits=16bits=2bytes.
How do i do that?

Don't.

You'll spend more time extracting/setting the bits. Just use 'char':

char a[4] = {1,2,3,4};

if you want to save space.

V
 
I

Ivan Vecerina

: int a[4]={1,2,3,4};
: but i d rather not use int (which on my machine is 4 bytes) I d like to
: store all 4 values in 4elements*4bits=16bits=2bytes.
: How do i do that?

You could try:

struct Quad4 {
unsigned a : 4; //allocate only 4 bits for this field
unsigned b : 4;
unsigned c : 4;
unsigned d : 4;
};

This is likely to do what you want on many platforms, but
implementation details and in-memory representation may vary.
Simply assigning/reading the fields of the above structure
will have the compiler pack/unpack the values for you
automatically.


Alternatively, you can manually control the packing with
something like:

unsigned pack(unsigned a, unsigned b, unsigned c, unsigned d)
{
return ((a&7)<<12)|((b&7)<<4)|((c&7)<<8)|(d&7);
}

....and store the packed value in a 16-bit unsigned
integer variable, then:

void unpack(unsigned v /*packed value*/
, unsigned& a, unsigned& b, unsigned& c, unsigned& d)
{
a = (v>>12)&7;
b = (v>> 8)&7;
c = (v>> 4)&7;
d = v &7;
}


hth --Ivan
 
M

Matt

I agree with Victor. Just use chars. The memory you will save isn't
worth the extra cycles unpacking will take up.
 
M

Mogens Heller Jensen

int a[4]={1,2,3,4};
but i d rather not use int (which on my machine is 4 bytes) I d like to
store all 4 values in 4elements*4bits=16bits=2bytes.
How do i do that?

When you propose a question like this, people will always give you their
opinions on what to do and what not to do. I think this is a precise
question which deserves an answer (and Ivan Vecerina gave you that).

However, if you want to avoid being "attacked" by people's opinions another
time, you could give some information on _why_ you want to do it like that.
Then people might have some useful ideas...

Just a thought :eek:)

The best,
Mogens
 
B

bluekite2000

i did as you said
struct Quad4{
unsigned char a:4;
unsigned char b:4;
unsigned char c:4;
unsigned char d:4;
};

int main (void)
{

Quad4 temp;
temp.a='1';
temp.b='2';
temp.c='3';
temp.d='4';

cout<<temp.a;
//nothing gets printed
return 0;
}

I ran the debugger and temp does have all values 1,2,3,4 but cout
doesnt work. I tried to do the same thing with unsigned int instead of
unsigned char and everything works fine. Any idea?
 
L

lpw

i did as you said
struct Quad4{
unsigned char a:4;
unsigned char b:4;
unsigned char c:4;
unsigned char d:4;
};

int main (void)
{

Quad4 temp;
temp.a='1';
temp.b='2';
temp.c='3';
temp.d='4';

cout<<temp.a;
//nothing gets printed
return 0;
}

I ran the debugger and temp does have all values 1,2,3,4 but cout
doesnt work. I tried to do the same thing with unsigned int instead of
unsigned char and everything works fine. Any idea?

Try this:

struct Quad4{
unsigned char a:4;
unsigned char b:4;
unsigned char c:4;
unsigned char d:4;
};

int main (void)
{

Quad4 temp;
temp.a=1;
temp.b=2;
temp.c=3;
temp.d=4;

cout<<(int)temp.a<<endl;
cout<<(int)temp.b<<endl;
cout<<(int)temp.c<<endl;
cout<<(int)temp.d<<endl;
return 0;
}

Your first problem was that you were assigning ASCII character codes which
didn't fit in the bit fields. Your second problem was that you were trying
to print unsigned chars, which were unprintable characters, thus didn't show
up.
 
G

Greg

int a[4]={1,2,3,4};
but i d rather not use int (which on my machine is 4 bytes) I d like to
store all 4 values in 4elements*4bits=16bits=2bytes.
How do i do that?

If the value ranges between 0 and 7 (inclusive), then it can be stored
in 4 bits, and still leave one bit to spare.

Why not economize still further and use only 3 bits to store the 3-bit
value?

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top