What is the best way to implement a bitwise memmove
? The method should take an additional destination and source bit-offset and the count should be in bits too.
Here is a partial implementation (not tested). There are obvious efficiency and usability improvements.
Copy n
bytes from src
to dest
(not overlapping src
), and shift bits at dest
rightwards by bit
bits, 0 <= bit
<= 7. This assumes that the least significant bits are at the right of the bytes
void memcpy_with_bitshift(unsigned char *dest, unsigned char *src, size_t n, int bit)
{
int i;
memcpy(dest, src, n);
for (i = 0; i < n; i++) {
dest[i] >> bit;
}
for (i = 0; i < n; i++) {
dest[i+1] |= (src[i] << (8 - bit));
}
}
Some improvements to be made:
bit
bits at beginning of dest
.