Thread overview
bit packing
Apr 14, 2002
Pavel Minayev
Apr 14, 2002
Russ Lewis
Apr 14, 2002
Walter
Apr 14, 2002
Pavel Minayev
Apr 14, 2002
Walter
April 14, 2002
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




April 14, 2002
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))) ]


April 14, 2002
"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.


April 14, 2002
"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.


April 14, 2002
"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>.