假设我有一个字符数组,并且我想将N位左移每个字节左边,所以只有第一个字符的N位会丢失。移位N位整个字符数组
举例:kxmo
移3位向左应该成为[email protected]
这是我现在,但它不是按预期工作:
#include <stdio.h>
int main(void) {
//shift the array with length *len* *shift* bits to the left
int len = 4, shift = 3;
unsigned char a[len] = "kxmo";
unsigned char b[len]; //[email protected]
unsigned char tmp = 0, tmp2 = 0;
for(int i = len - 1; i > 0; i--) {
tmp = 0 | (a[i] << shift);
b[i] = a[i];
tmp2 = 0 | (a[i - 1] << shift);
b[i - 1] = (a[i - 1] << shift)^tmp;
}
printf("old: %s | new: %s\n", a, b);
return 0;
}
我在哪里失败?
编辑:
这就是我得到现在:old: kxmo | new: �xmo
什么问题?你在调试器中得到什么?你有什么自己尝试过的? – Olaf
@Olaf我得到' xmo',这不是我所期待的。我已经尝试了我在我的问题中编写的代码。 – alexandernst
'0 |无论与“无论”都一样。你是如何计算样本结果的?你的代码是否一样?提示:XOR在这里没用。 – Olaf