473,395 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
7 1178
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
<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
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

<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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Shailesh Humbad | last post by:
I wrote a short page as a quick reference to c++ integer data types. Any feedback welcome: http://www.somacon.com/blog/page11.php
27
by: geskerrett | last post by:
I am hoping someone can help me solve a bit of a puzzle. We are working on a data file reader and extraction tool for an old MS-DOS accounting system dating back to the mid 80's. In the data...
4
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system...
18
by: Ptp | last post by:
is there a integer data type of one byte in gcc? I know delphi have fundamental integer types include byte, shortint... the byte types is unsigned 8-bit, such as below: procedure...
9
by: Ralf Hildebrandt | last post by:
Hi all! First of all: I am a C-newbie. I have noticed a "strange" behavior with the standart integer multiplication. The code is: void main(void)
10
by: vb | last post by:
Hi all, I am a newbie in C and i want to know what all pointer conversions are "legal" according to ANSI C standard. For Example, int* to char*, some_struct* to char* and so on .. According to...
76
by: KimmoA | last post by:
First of all: I love C and think that it's beautiful. However, there is at least one MAJOR flaw: the lack of a boolean type. OK. Some of you might refer to C99 and its _Bool (what's up with the...
8
by: brainflakes.org | last post by:
Hi guys, I need to manipulate binary data (8 bit) stored in a 2 dimensional array. I've tried various methods (arrays, using a string filled with chr(0), using gd lib) and so far the fastest...
2
by: kikkubey | last post by:
Dear all, I have 4 numbers (A) stored 10 bits data. the first 8 bits data stored in 1 byte data arrays (B). the last 2bits of each A stored sequentially in 1 byte number C (the first two bits of C...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.