repack ubyte[] to use only 7 bits

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Dec 6 15:01:07 PST 2014


Charles Hixson:

> byte[]    x8to7 (ubyte[] bin)

Better to add some annotations, like pure, @safe, nothrow, if you 
can, and to annotate the bin with an "in".


>     int    fByte, fBit;

It's probably better to define them as size_t.



>         switch (fBit)

I think D doesn't yet allow this switch to be _meaningfully_ a 
final switch.


>                 b    =    bin[fByte] & 0x7f;

D allows binary number literals as 0b100110010101.


>                 b    ~=    (b1 | b2);

Perhaps an output range is better?


>         if    (b == 0)    bout    ~=    0x80;
>         else            bout    ~=    b;
>         fBit    =    fBit + 7;
>         if    (fBit > 7)
>         {    fByte++;
>             fBit -=    7;

The formatting seems a bit messy.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list