www.digitalmars.com         C & C++   DMDScript  

D - bit packing

reply "Pavel Minayev" <evilone omen.ru> writes:
Just found out that struct consisting of N bit fields is N bytes long:

    struct bits { bit a, b, c }    // bits.length == 3

Why? Since language forbids pointers to bits, why not pack them
as tight as it's possible? D does it in bit arrays, but not in
structs.

I can understand perfomance reasons; then, make some
attribute for this. align(0), maybe?

    struct bits { align(0) bit a, b, c; }    // bits.length == 1
Apr 14 2002
next sibling parent Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Pavel Minayev wrote:

 I can understand perfomance reasons; then, make some
 attribute for this. align(0), maybe?

     struct bits { align(0) bit a, b, c; }    // bits.length == 1
Would probably be clearer if you used struct bits { align(bit) bit a, b, c; } but otherwise a very good idea. -- The Villagers are Online! http://villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ]
Apr 14 2002
prev sibling parent reply "Walter" <walter digitalmars.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:a9bpjf$134d$1 digitaldaemon.com...
 Just found out that struct consisting of N bit fields is N bytes long:

     struct bits { bit a, b, c }    // bits.length == 3

 Why? Since language forbids pointers to bits, why not pack them
 as tight as it's possible? D does it in bit arrays, but not in
 structs.

 I can understand perfomance reasons; then, make some
 attribute for this. align(0), maybe?

     struct bits { align(0) bit a, b, c; }    // bits.length == 1
It's an implementation issue whether individual bits get packed or not. The main reason for not doing it at the moment is it adds some implementation complexity. As you pointed out, you can always use bit arrays which will pack them regardless.
Apr 14 2002
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Walter" <walter digitalmars.com> wrote in message
news:a9clk5$1sfj$1 digitaldaemon.com...

 It's an implementation issue whether individual bits get packed or not.
The Does this statement apply to bit arrays as well, or they are always packed?
 main reason for not doing it at the moment is it adds some implementation
 complexity. As you pointed out, you can always use bit arrays which will
 pack them regardless.
Yep, right... but arrays are indexed with integers, and I wanted to have some descriptive names. The code was: class MouseEvent { struct Buttons { bit left, right, middle; } Buttons buttons; ... } I don't really care much if it takes 4 bytes instead of 1... but the latter is preferred, of course.
Apr 14 2002
parent "Walter" <walter digitalmars.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:a9co75$2022$1 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote in message
 news:a9clk5$1sfj$1 digitaldaemon.com...
 It's an implementation issue whether individual bits get packed or not.
The Does this statement apply to bit arrays as well, or they are always
packed? They're always packed.
 main reason for not doing it at the moment is it adds some
implementation
 complexity. As you pointed out, you can always use bit arrays which will
 pack them regardless.
Yep, right... but arrays are indexed with integers, and I wanted to have some descriptive names. The code was: class MouseEvent { struct Buttons { bit left, right, middle; } Buttons buttons; ... } I don't really care much if it takes 4 bytes instead of 1... but the latter is preferred, of course.
Of course <g>.
Apr 14 2002