By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,905 Members | 1,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,905 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?

Nov 1 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
bl**********@gmail.com wrote:
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
Nov 1 '05 #2

P: n/a
<bl**********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
: 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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Nov 1 '05 #3

P: n/a
I agree with Victor. Just use chars. The memory you will save isn't
worth the extra cycles unpacking will take up.

Nov 1 '05 #4

P: n/a

<bl**********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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 :o)

The best,
Mogens
Nov 1 '05 #5

P: n/a
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?

Nov 1 '05 #6

P: n/a
lpw
<bl**********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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.
Nov 1 '05 #7

P: n/a

bl**********@gmail.com wrote:
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

Nov 2 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.