Help needed writing bit fields

51 views
Skip to first unread message

Harry Potter

unread,
Sep 21, 2021, 1:58:04 PM9/21/21
to
Hi! I am working on file compression. Up to now, I have been writing to a bit stream the wrong way, i.e. starting at bit 0. Now, I need to write it starting at bit 7. Can somebody supply the p-code on how to do it?

James D. Allen

unread,
Oct 21, 2021, 1:25:20 AM10/21/21
to
On Wednesday, September 22, 2021 at 12:58:04 AM UTC+7, Harry Potter wrote:
> Hi! I am working on file compression. Up to now, I have been writing to a bit stream the wrong way, i.e. starting at bit 0. Now, I need to write it starting at bit 7. Can somebody supply the p-code on how to do it?

Bits within bytes:
It does not matter whether you treat Bit 0 or Bit 7 first, as long as your decompressor
and compressor use the same approach.

If you have working code and insist on reversing the sense, a table look-up might be the
easiest way.
unsigned char reverser[256] = {0x00, 0x80, 0x40, 0xc0, 0x20, ...

Bytes within larger units (e.g. uint32):
This is a more serious issue, if you want to be able to compress on a Big-endian machine
and decompress on Little-Endian (or vice versa). Processing the file byte-by-byte is the
sure-fire way to bypass the issue.

Assuming C programming, do NOT use C's built-in bitfields to address these issues!
Different compilers may pack those bits in different ways.

Cheers,
James
Reply all
Reply to author
Forward
0 new messages