本文介绍了按位的memmove的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是实现按位的最佳方式 memmove与
?该方法应该采取额外的目标和源位偏移和数量应该是位了。
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.
- 我看到ARM提供了非标准的,这不正是我需要的,但我找不到它的来源。
- 绑定的BitSet中包含的,但它的效率不高
- 我知道,C标准库不提供这样的方法,但我也无法找到任何第三方code提供了类似的方法。
推荐答案
下面是部分实现(未测试)。有明显的效率和可用性方面的改进。
Here is a partial implementation (not tested). There are obvious efficiency and usability improvements.
复制 N
从的src
字节来 DEST
(不重叠的src
),并在 DEST
移位位向右按位
位,0℃= 位
< = 7,这是假设至少显著位是在字节的权
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:
- 请不要覆盖第一个
位
在开始DEST
位。 - 合并循环
- 有办法由8 来复制数不能整除的位
- 在一个char修正了> 8位
- Don't overwrite first
bit
bits at beginning ofdest
. - Merge loops
- Have a way to copy a number of bits not divisible by 8
- Fix for >8 bits in a char
这篇关于按位的memmove的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!