Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Hurray for pointers! </p> <p>This code works by looking ahead 12 bits for each byte and copying the proper bits forward. 12 bits is the bottom half (nybble) of the next byte and the top half of 2 bytes away.</p> <pre><code>unsigned char length = 10; unsigned char data[10] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0A,0xBC}; unsigned char *shift = data; while (shift &lt; data+(length-2)) { *shift = (*(shift+1)&amp;0x0F)&lt;&lt;4 | (*(shift+2)&amp;0xF0)&gt;&gt;4; shift++; } *(data+length-2) = (*(data+length-1)&amp;0x0F)&lt;&lt;4; *(data+length-1) = 0x00; </code></pre> <blockquote> <p>Justin wrote:<br> @Mike, your solution works, but does not carry. </p> </blockquote> <p>Well, I'd say a normal shift operation does just that (called overflow), and just lets the extra bits fall off the right or left. It's simple enough to carry if you wanted to - just save the 12 bits before you start to shift. Maybe you want a circular shift, to put the overflowed bits back at the bottom? Maybe you want to realloc the array and make it larger? Return the overflow to the caller? Return a boolean if non-zero data was overflowed? You'd have to define what carry means to you.</p> <pre><code>unsigned char overflow[2]; *overflow = (*data&amp;0xF0)&gt;&gt;4; *(overflow+1) = (*data&amp;0x0F)&lt;&lt;4 | (*(data+1)&amp;0xF0)&gt;&gt;4; while (shift &lt; data+(length-2)) { /* normal shifting */ } /* now would be the time to copy it back if you want to carry it somewhere */ *(data+length-2) = (*(data+length-1)&amp;0x0F)&lt;&lt;4 | (*(overflow)&amp;0x0F); *(data+length-1) = *(overflow+1); /* You could return a 16-bit carry int, * but endian-ness makes that look weird * if you care about the physical layout */ unsigned short carry = *(overflow+1)&lt;&lt;8 | *overflow; </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. 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