public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* sizeof bit-fields
@ 2003-10-16 11:13 Massimiliano Cialdi
  2003-10-16 12:26 ` Eljay Love-Jensen
  0 siblings, 1 reply; 6+ messages in thread
From: Massimiliano Cialdi @ 2003-10-16 11:13 UTC (permalink / raw)
  To: gcc help

I tried to declare a bit-filed in this way:

struct rec
{
	unsigned short a : 10;
	unsigned short b : 10;
	unsigned short c : 10;
	unsigned short d : 10;
};

40 bits = 5 byte.
but sizeof(struct rec) is 8.

How can I obtain a 5 bytes long structure (if it is possible)? 

thanks
-- 
Massimiliano Cialdi
cialdi@firenze.net
m.cialdi@oksys.it

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: sizeof bit-fields
  2003-10-16 11:13 sizeof bit-fields Massimiliano Cialdi
@ 2003-10-16 12:26 ` Eljay Love-Jensen
  2003-10-17  6:08   ` Re[2]: " Christian Schäfer
  0 siblings, 1 reply; 6+ messages in thread
From: Eljay Love-Jensen @ 2003-10-16 12:26 UTC (permalink / raw)
  To: Massimiliano Cialdi, gcc help

Hi Massimiliano,

>How can I obtain a 5 bytes long structure (if it is possible)? 

The bit-fields are going to pack like this in memory (probably), big-endian representation on a 32-bit/word machine:
  +--3---------2---------1---------+
0 +..ccccccccccbbbbbbbbbbaaaaaaaaaa+
4 +......................dddddddddd+
  +--------------------------------+

So the 32-bit word at offset 0 has a, b, and c and 2 fallow bits.  The 32-bit word at offset 4 has d and 22 fallow bits.

Note that bit-fields (typically) do not span across words.

If you really want the structure to be 5 bytes AND have byte alignment, you'll have to do more of the work yourself.  For example, in C++, I'd do it this way:

typedef char byte;
struct rec
{
    byte m[5]; // 40-bits to pack our 4 10-bit unsigned values.

    void setA(unsigned i_10);
    void setB(unsigned i_10);
    void setC(unsigned i_10);
    void setD(unsigned i_10);
    void setE(unsigned i_10);

    unsigned getA() const;
    unsigned getB() const;
    unsigned getC() const;
    unsigned getD() const;
    unsigned getE() const;
};

void rec::setA(unsigned i_10)
{
    m[0] = i_10 & 0xFF;
    m[1] = (m[1] & 0xFC) | ((i_10 >> 8) & 0x03);
}

unsigned rec::getA() const
{
    return (m[1] & 0x03) | (m[0] & 0xFF);
}

I leave get/set B, C, D as an exercise.  Note that the compiler's optimizer will optimize the seemingly superfluous masking where it's unnecessary, which makes this stuff fairly platform agnostic.

Another advantage is that you can read/write this structure directly to file or over a socket, on any 8-bit/byte platform, and it'll work.

HTH,
--Eljay


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re[2]: sizeof bit-fields
  2003-10-16 12:26 ` Eljay Love-Jensen
@ 2003-10-17  6:08   ` Christian Schäfer
  2003-10-17 13:28     ` Eljay Love-Jensen
  0 siblings, 1 reply; 6+ messages in thread
From: Christian Schäfer @ 2003-10-17  6:08 UTC (permalink / raw)
  To: gcc help

hi Eljay,

Thursday, October 16, 2003, 2:25:26 PM, you wrote:
> Hi Massimiliano,

>>How can I obtain a 5 bytes long structure (if it is possible)? 

> If you really want the structure to be 5 bytes AND have byte
> alignment, you'll have to do more of the work yourself.
> For example, in C++, I'd do it this way:
> ...

what exactly is c++ about your code? what would not work with pure c?
I've seen structs with functions in c..



 
gruss
/Christian                   mailto:caefer@krachstoff.net

---


I propose that the following character sequence for joke markers:

:-)

                             19-Sep-82 11:44    Scott E  Fahlman

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re[2]: sizeof bit-fields
  2003-10-17  6:08   ` Re[2]: " Christian Schäfer
@ 2003-10-17 13:28     ` Eljay Love-Jensen
  2003-10-17 15:42       ` Re[3]: " Christian Schäfer
  0 siblings, 1 reply; 6+ messages in thread
From: Eljay Love-Jensen @ 2003-10-17 13:28 UTC (permalink / raw)
  To: Christian Schäfer, gcc help

Hi Christian,

>what exactly is c++ about your code? what would not work with pure c?  I've seen structs with functions in c..

I did not know that C has instance methods for structures.  That's a new one on me.

I haven't programmed in C since 1989.

--Eljay


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re[3]: sizeof bit-fields
  2003-10-17 13:28     ` Eljay Love-Jensen
@ 2003-10-17 15:42       ` Christian Schäfer
  2003-10-17 16:27         ` Eljay Love-Jensen
  0 siblings, 1 reply; 6+ messages in thread
From: Christian Schäfer @ 2003-10-17 15:42 UTC (permalink / raw)
  To: gcc help

hi Eljay,

>>what exactly is c++ about your code? what would not work with pure c?  I've seen structs with functions in c..

> I did not know that C has instance methods for structures.  That's a new one on me.

well, at least you have function pointers, right?


 
gruss
/Christian                   mailto:caefer@krachstoff.net

---


I propose that the following character sequence for joke markers:

:-)

                             19-Sep-82 11:44    Scott E  Fahlman

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: sizeof bit-fields
  2003-10-17 15:42       ` Re[3]: " Christian Schäfer
@ 2003-10-17 16:27         ` Eljay Love-Jensen
  0 siblings, 0 replies; 6+ messages in thread
From: Eljay Love-Jensen @ 2003-10-17 16:27 UTC (permalink / raw)
  To: Christian Schäfer, gcc help

Hi Christian,

>well, at least you have function pointers, right?

Function pointers are a great deal different from object methods.

Especially since if you put function pointers in the structure, it will make the structure more than 5 bytes big (which is one of the criteria).

And I don't think C allows static function pointers that are part of (or associated with) a struct.

Note -- a person can do object-oriented programming with C.  Case in point, the POV ray-tracer.  But the language (at least, C89) doesn't facilitate object-oriented programming.

But, as I've said, I haven't programmed in C since 1989, and C99 may have incorporated some new-and-interesting programming techniques/paradigms.

Sincerely,
--Eljay


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2003-10-17 16:27 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-10-16 11:13 sizeof bit-fields Massimiliano Cialdi
2003-10-16 12:26 ` Eljay Love-Jensen
2003-10-17  6:08   ` Re[2]: " Christian Schäfer
2003-10-17 13:28     ` Eljay Love-Jensen
2003-10-17 15:42       ` Re[3]: " Christian Schäfer
2003-10-17 16:27         ` Eljay Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).