Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Bit filed usage

Reply
Thread Tools

Bit filed usage

 
 
Capstar
Guest
Posts: n/a
 
      08-08-2003
Hi NG,

I have a question about the usage of bitfileds in a structure.
for instance:

struct pcard
{
unsigned pips : 4;
unsigned suit : 2;
};

The only reason I see fot using this is to save memory. But isn't it true
that on most systems memory isn't that much of an issue to pack data like
this? And even on some embedded machine whitout much memory, I'm not sure
the memory gain by packing your structures will be much bigger than the loss
you get from the increased code size. Maybe when you have lots of these
structures you will gain something.

Another reason I would consider this helpfull would be if the structures
would be written to some binary file. In my opinion file size does matter.
The only problem I see in this case is that every machine, and even a
different compiler at the same machine, could map the bits somewhere else in
a word. This would mean that the file is incompatible with anything but the
same compiler and the same machine.

So I am very curious if anybody ever uses these kind of structures and where
they are used for.

Mark


 
Reply With Quote
 
 
 
 
Simon Wilkins
Guest
Posts: n/a
 
      08-08-2003
"Capstar" <spam0@@@eg.homeip.net> wrote in message
news:bgvvu3$ac0$(E-Mail Removed)...
> Hi NG,
>
> I have a question about the usage of bitfileds in a structure.
> for instance:
>
> struct pcard
> {
> unsigned pips : 4;
> unsigned suit : 2;
> };
>
> The only reason I see fot using this is to save memory. But isn't it

true
> that on most systems memory isn't that much of an issue to pack data

like
> this? And even on some embedded machine whitout much memory, I'm not

sure
> the memory gain by packing your structures will be much bigger than

the loss
> you get from the increased code size. Maybe when you have lots of

these
> structures you will gain something.
>
> Another reason I would consider this helpfull would be if the

structures
> would be written to some binary file. In my opinion file size does

matter.
> The only problem I see in this case is that every machine, and even a
> different compiler at the same machine, could map the bits somewhere

else in
> a word. This would mean that the file is incompatible with anything

but the
> same compiler and the same machine.
>
> So I am very curious if anybody ever uses these kind of structures and

where
> they are used for.
>
> Mark
>


I suspect this is off-topic, but we use bit-field structures when
mapping data for RFID tags where the memory is usually measured in bits,
not bytes let alone kB.

--
Simon Wilkins
www.robotag.co.uk

 
Reply With Quote
 
 
 
 
Pieter Droogendijk
Guest
Posts: n/a
 
      08-08-2003
On Fri, 8 Aug 2003 13:01:20 +0200
"Capstar" <spam0@@@eg.homeip.net> wrote:
> struct pcard
> {
> unsigned pips : 4;
> unsigned suit : 2;
> };
>
> The only reason I see fot using this is to save memory. But isn't it true
> that on most systems memory isn't that much of an issue to pack data like
> this? And even on some embedded machine whitout much memory, I'm not sure
> the memory gain by packing your structures will be much bigger than the loss
> you get from the increased code size. Maybe when you have lots of these
> structures you will gain something.


A reason for using these is to get around those annoying bitwise operations.
They can be a real pain, and even when macro-ifying the operations, they don't
improve readability.
Bitfields leave those operations to the compiler.

And the memory conservation is not an issue, since the structs are aligned
anyway. The example you gave won't take up just 6 bits, it'll align and take up
a byte, or a word, or an int, depending on your platform.

Just using a struct of char's uses only slightly more memory, but gets rid of
all the operations. All depends on what you want, more memory or more cycles.

--
char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
c,k,0),x(c,k+=*s-c,s+1),*k)x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
 
Reply With Quote
 
Capstar
Guest
Posts: n/a
 
      08-08-2003

"Pieter Droogendijk" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) g...
> On Fri, 8 Aug 2003 13:01:20 +0200
> "Capstar" <spam0@@@eg.homeip.net> wrote:
> > struct pcard
> > {
> > unsigned pips : 4;
> > unsigned suit : 2;
> > };
> >
> > The only reason I see fot using this is to save memory. But isn't it

true
> > that on most systems memory isn't that much of an issue to pack data

like
> > this? And even on some embedded machine whitout much memory, I'm not

sure
> > the memory gain by packing your structures will be much bigger than the

loss
> > you get from the increased code size. Maybe when you have lots of these
> > structures you will gain something.

>
> A reason for using these is to get around those annoying bitwise

operations.
> They can be a real pain, and even when macro-ifying the operations, they

don't
> improve readability.
> Bitfields leave those operations to the compiler.


Yes I understand, but what do you need those bitwise operations for if you
don't use bits anyway? I mean bits in the sence that you don't measure how
many bits you need for something and then stuff as many as possible in one
word, but just use a word or more for every variable. That would also get
you around those bitwise operations.

>
> And the memory conservation is not an issue, since the structs are aligned
> anyway. The example you gave won't take up just 6 bits, it'll align and

take up
> a byte, or a word, or an int, depending on your platform.
>
> Just using a struct of char's uses only slightly more memory, but gets rid

of
> all the operations. All depends on what you want, more memory or more

cycles.
>
> --
>

char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x
(
> c,k,0),x(c,k+=*s-c,s+1),*k)x(*s,k,s+1),0));else

c=10;printf(&x(~0,0,k)[c-~-
>

c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");
}


 
Reply With Quote
 
Capstar
Guest
Posts: n/a
 
      08-08-2003

"Zoran Cutura" <(E-Mail Removed)> wrote in message
news:bh02gm$plc$(E-Mail Removed)-felb.debis.de...
> Capstar <spam0@@@eg.homeip.net> wrote:
> > Hi NG,
> >
> > I have a question about the usage of bitfileds in a structure.
> > for instance:
> >
> > struct pcard
> > {
> > unsigned pips : 4;
> > unsigned suit : 2;
> > };
> >
> > The only reason I see fot using this is to save memory. But isn't it

true
> > that on most systems memory isn't that much of an issue to pack data

like
> > this? And even on some embedded machine whitout much memory, I'm not

sure
> > the memory gain by packing your structures will be much bigger than the

loss
> > you get from the increased code size. Maybe when you have lots of these
> > structures you will gain something.
> >
> > Another reason I would consider this helpfull would be if the structures
> > would be written to some binary file. In my opinion file size does

matter.
> > The only problem I see in this case is that every machine, and even a
> > different compiler at the same machine, could map the bits somewhere

else in
> > a word. This would mean that the file is incompatible with anything but

the
> > same compiler and the same machine.
> >
> > So I am very curious if anybody ever uses these kind of structures and

where
> > they are used for.

>
> Your understanding of what the most systems are is, IMHO, wrong. Have
> you ever noticed that there are so many embedded devices out there
> having only a few kBytes of RAM available? For example in automotive
> typical so called ECU's (electronic control units) run with controllers
> having between 120 and 500 kBytes of ROM where the program code can be
> stored in, but only with 2 to 16 kBytes of RAM that can be used for
> data. That usually brings in the need to care about Bits and Bytes.


I must admit that there are a lot of embedded systems around nowadays, but I
didn't realise that the ROM is often that much bigger than the RAM. I
actually assumed the other way around. Which is probably a bit naive since
ROM is much cheaper than RAM.
So this actually is a good answer on my question why to use bitfield
operators.

>
> You're probably thinking about most programmers being programmig for

systems
> where memory isn't a issue, but that is a claim that is hard to prove.
>
> Anyway, when you start a project or program, you usually do not need to
> care about memory usage and speed form the very start. It should be
> sufficient to make the program functionally correct and care about size
> and speed when you recognize that there are problems with these.
>
> As you may have recognized yourself, structures shouldn't be written to
> binary files as they are. Structures are very implementation specific
> and as you say even two versions of a compiler may constitute 'em in
> different ways. That is an issue with all structs not only bit fields.


I understand. I didn't mean writing structs to a binary file, but for
instance creating a one word header in a file. If that file contains some
flags, you could use masks and bit operations to set and clear flags, or use
a bitfield structure, and use that. Well actually the last is not possible,
but that was what I tried to say in my OP.

Mark


 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      08-08-2003

On Fri, 8 Aug 2003, Capstar wrote:
>
> "Pieter Droogendijk" <(E-Mail Removed)> wrote...
> > "Capstar" <spam0@@@eg.homeip.net> wrote:
> > > struct pcard
> > > {
> > > unsigned pips : 4;
> > > unsigned suit : 2;
> > > };

> >
> > A reason for using these is to get around those annoying bitwise
> > operations. They can be a real pain, and even when macro-ifying the
> > operations, they don't improve readability.
> > Bitfields leave those operations to the compiler.

>
> Yes I understand, but what do you need those bitwise operations for if you
> don't use bits anyway? I mean bits in the sence that you don't measure how
> many bits you need for something and then stuff as many as possible in one
> word, but just use a word or more for every variable. That would also get
> you around those bitwise operations.


There are some applications for bitwise operators;
encryption and compression come to mind. IIRC,
some schemes for data encryption require bitwise
XORs and shifts on 24-bit data, which could be a
little annoying on platforms with 16- and 32-bit
words. So if one really wanted to be pedantic,
one could write

struct int24_s
{
unsigned value: 24;
};

typedef struct int24_s int24_s;

#define VALUE(int24) ((int24).value)

and then use the regular bitwise operators on
the VALUEs of int24_s objects, without worrying
about bit masks.

The caveat here is that I'm not sure whether
the above bit-field definition is guaranteed to
work on platforms where 'unsigned int' has fewer
than 24 value bits. Expert advice welcome.

-Arthur

 
Reply With Quote
 
John Devereux
Guest
Posts: n/a
 
      08-08-2003
Zoran Cutura <(E-Mail Removed)> writes:

> Capstar <spam0@@@eg.homeip.net> wrote:
> > Hi NG,
> >
> > I have a question about the usage of bitfileds in a structure.
> > for instance:
> >
> > struct pcard
> > {
> > unsigned pips : 4;
> > unsigned suit : 2;
> > };
> >
> > The only reason I see fot using this is to save memory. But isn't it true
> > that on most systems memory isn't that much of an issue to pack data like
> > this? And even on some embedded machine whitout much memory, I'm not sure
> > the memory gain by packing your structures will be much bigger than the loss
> > you get from the increased code size. Maybe when you have lots of these
> > structures you will gain something.
> >
> > Another reason I would consider this helpfull would be if the structures
> > would be written to some binary file. In my opinion file size does matter.
> > The only problem I see in this case is that every machine, and even a
> > different compiler at the same machine, could map the bits somewhere else in
> > a word. This would mean that the file is incompatible with anything but the
> > same compiler and the same machine.
> >
> > So I am very curious if anybody ever uses these kind of structures and where
> > they are used for.

>
> Your understanding of what the most systems are is, IMHO, wrong. Have
> you ever noticed that there are so many embedded devices out there
> having only a few kBytes of RAM available? For example in automotive
> typical so called ECU's (electronic control units) run with controllers
> having between 120 and 500 kBytes of ROM where the program code can be
> stored in, but only with 2 to 16 kBytes of RAM that can be used for
> data. That usually brings in the need to care about Bits and Bytes.


In fact, I suspect that when C bitfields were "invented", the machines of the
time would have been equivalent in resources to the embedded systems of which you
speak.

Are there any legitimate uses other than saving space in structures? I
have seen them used for defining peripheral hardware register layouts, but that
is very bad form here so I won't even mention it

--

John Devereux
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      08-09-2003
On Fri, 8 Aug 2003 13:01:20 +0200, "Capstar" <spam0@@@eg.homeip.net>
wrote in comp.lang.c:

> Hi NG,
>
> I have a question about the usage of bitfileds in a structure.
> for instance:
>
> struct pcard
> {
> unsigned pips : 4;
> unsigned suit : 2;
> };
>
> The only reason I see fot using this is to save memory. But isn't it true
> that on most systems memory isn't that much of an issue to pack data like
> this? And even on some embedded machine whitout much memory, I'm not sure
> the memory gain by packing your structures will be much bigger than the loss
> you get from the increased code size. Maybe when you have lots of these
> structures you will gain something.


Have you ever worked on an embedded system without much memory? If
you haven't had the experience, your experience is pretty useless.
Try writing C code to run in an environment with 4K (4K octets or
bytes, not kilobytes or megabytes) read-only code space and less than
128 8-bit bytes of read/write RAM.

Then you'll be qualified to have an _informed_ opinion about writing C
code for such environments.

> Another reason I would consider this helpfull would be if the structures
> would be written to some binary file. In my opinion file size does matter.
> The only problem I see in this case is that every machine, and even a
> different compiler at the same machine, could map the bits somewhere else in
> a word. This would mean that the file is incompatible with anything but the
> same compiler and the same machine.
>
> So I am very curious if anybody ever uses these kind of structures and where
> they are used for.
>
> Mark


If you don't see the usefulness of bit-fields in the code your write
for the execution environments your code runs in, by all means feel
free not to use them.

Nobody ever said you had to use them, or even suggested that you use
them.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      08-09-2003
On 8 Aug 2003 11:45:26 GMT, Zoran Cutura
<(E-Mail Removed)> wrote in comp.lang.c:

> Capstar <spam0@@@eg.homeip.net> wrote:
> > Hi NG,
> >
> > I have a question about the usage of bitfileds in a structure.
> > for instance:
> >
> > struct pcard
> > {
> > unsigned pips : 4;
> > unsigned suit : 2;
> > };
> >
> > The only reason I see fot using this is to save memory. But isn't it true
> > that on most systems memory isn't that much of an issue to pack data like
> > this? And even on some embedded machine whitout much memory, I'm not sure
> > the memory gain by packing your structures will be much bigger than the loss
> > you get from the increased code size. Maybe when you have lots of these
> > structures you will gain something.
> >
> > Another reason I would consider this helpfull would be if the structures
> > would be written to some binary file. In my opinion file size does matter.
> > The only problem I see in this case is that every machine, and even a
> > different compiler at the same machine, could map the bits somewhere else in
> > a word. This would mean that the file is incompatible with anything but the
> > same compiler and the same machine.
> >
> > So I am very curious if anybody ever uses these kind of structures and where
> > they are used for.

>
> Your understanding of what the most systems are is, IMHO, wrong. Have
> you ever noticed that there are so many embedded devices out there
> having only a few kBytes of RAM available? For example in automotive


"kBytes"? That's among the larger size for embedded systems. I've
done quite a few applications in my time with C for 8051 derivative's
with 128 bytes, some of which has to be left for CPU registers.

> typical so called ECU's (electronic control units) run with controllers
> having between 120 and 500 kBytes of ROM where the program code can be
> stored in, but only with 2 to 16 kBytes of RAM that can be used for
> data. That usually brings in the need to care about Bits and Bytes.
>
> You're probably thinking about most programmers being programmig for systems
> where memory isn't a issue, but that is a claim that is hard to prove.
>
> Anyway, when you start a project or program, you usually do not need to
> care about memory usage and speed form the very start. It should be
> sufficient to make the program functionally correct and care about size
> and speed when you recognize that there are problems with these.
>
> As you may have recognized yourself, structures shouldn't be written to
> binary files as they are. Structures are very implementation specific
> and as you say even two versions of a compiler may constitute 'em in
> different ways. That is an issue with all structs not only bit fields.


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
Reply With Quote
 
Don Starr
Guest
Posts: n/a
 
      08-09-2003
On Sat, 09 Aug 2003 18:32:07 GMT, Jack Klein <(E-Mail Removed)> wrote:

>Try writing C code to run in an environment with 4K (4K octets or
>bytes, not kilobytes or megabytes) read-only code space and less than
>128 8-bit bytes of read/write RAM.


Now I feel spoiled with my 8k-word ROM, 384-byte RAM PIC

(And, yes, I use bitfields extensively. I even use them to <gasp!> control bits in hardware
registers.)

-Don

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the point of having 16 bit colour if a computer monitor can only display 8 bit colour? How do you edit 16 bit colour when you can only see 8 bit? Scotius Digital Photography 6 07-13-2010 03:33 AM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd Computer Support 0 09-17-2004 08:15 PM
COMPLAINT FILED CITIZEN OF INDIA MCSE 30 07-02-2004 05:13 AM
application filed to initialize properly Patricia Kline ASP .Net 0 02-26-2004 10:33 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM



Advertisments