Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is what I ended up doing. (<strong>EDIT</strong> Changed on 8/21/2014 for a single bit copy bug.)</p> <pre><code>#include &lt;limits.h&gt; #include &lt;string.h&gt; #include &lt;stddef.h&gt; #define PREPARE_FIRST_COPY() \ do { \ if (src_len &gt;= (CHAR_BIT - dst_offset_modulo)) { \ *dst &amp;= reverse_mask[dst_offset_modulo]; \ src_len -= CHAR_BIT - dst_offset_modulo; \ } else { \ *dst &amp;= reverse_mask[dst_offset_modulo] \ | reverse_mask_xor[dst_offset_modulo + src_len]; \ c &amp;= reverse_mask[dst_offset_modulo + src_len]; \ src_len = 0; \ } } while (0) static void bitarray_copy(const unsigned char *src_org, int src_offset, int src_len, unsigned char *dst_org, int dst_offset) { static const unsigned char mask[] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff }; static const unsigned char reverse_mask[] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff }; static const unsigned char reverse_mask_xor[] = { 0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01, 0x00 }; if (src_len) { const unsigned char *src; unsigned char *dst; int src_offset_modulo, dst_offset_modulo; src = src_org + (src_offset / CHAR_BIT); dst = dst_org + (dst_offset / CHAR_BIT); src_offset_modulo = src_offset % CHAR_BIT; dst_offset_modulo = dst_offset % CHAR_BIT; if (src_offset_modulo == dst_offset_modulo) { int byte_len; int src_len_modulo; if (src_offset_modulo) { unsigned char c; c = reverse_mask_xor[dst_offset_modulo] &amp; *src++; PREPARE_FIRST_COPY(); *dst++ |= c; } byte_len = src_len / CHAR_BIT; src_len_modulo = src_len % CHAR_BIT; if (byte_len) { memcpy(dst, src, byte_len); src += byte_len; dst += byte_len; } if (src_len_modulo) { *dst &amp;= reverse_mask_xor[src_len_modulo]; *dst |= reverse_mask[src_len_modulo] &amp; *src; } } else { int bit_diff_ls, bit_diff_rs; int byte_len; int src_len_modulo; unsigned char c; /* * Begin: Line things up on destination. */ if (src_offset_modulo &gt; dst_offset_modulo) { bit_diff_ls = src_offset_modulo - dst_offset_modulo; bit_diff_rs = CHAR_BIT - bit_diff_ls; c = *src++ &lt;&lt; bit_diff_ls; c |= *src &gt;&gt; bit_diff_rs; c &amp;= reverse_mask_xor[dst_offset_modulo]; } else { bit_diff_rs = dst_offset_modulo - src_offset_modulo; bit_diff_ls = CHAR_BIT - bit_diff_rs; c = *src &gt;&gt; bit_diff_rs &amp; reverse_mask_xor[dst_offset_modulo]; } PREPARE_FIRST_COPY(); *dst++ |= c; /* * Middle: copy with only shifting the source. */ byte_len = src_len / CHAR_BIT; while (--byte_len &gt;= 0) { c = *src++ &lt;&lt; bit_diff_ls; c |= *src &gt;&gt; bit_diff_rs; *dst++ = c; } /* * End: copy the remaing bits; */ src_len_modulo = src_len % CHAR_BIT; if (src_len_modulo) { c = *src++ &lt;&lt; bit_diff_ls; c |= *src &gt;&gt; bit_diff_rs; c &amp;= reverse_mask[src_len_modulo]; *dst &amp;= reverse_mask_xor[src_len_modulo]; *dst |= c; } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload