这是一个完整且有效的解决方案。留给读者作为练习的小草率:)
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define M_SIZE 100
#define N_SIZE 25
#define bitsToBytes(n) ((n + 7)/8)
typedef unsigned char byte;
void dumpBytes(byte *arr, size_t size) {
int b;
for (b=0; b<size; b++) {
printf("%02x", *arr++);
}
printf("\n");
}
int main(int argc, char *argv[]) {
byte M[bitsToBytes(M_SIZE)], N[bitsToBytes(N_SIZE)];
/* Fill M and N with random bits */
int b;
for (b=0; b<sizeof(M); b++) {
M[b] = 0xff & rand();
}
for (b=0; b<sizeof(N); b++) {
N[b] = 0xff & rand();
}
/* Create a couple of arrays big enough for M_SIZE + N_SIZE,
to allow shifting N all the way before the left of M. */
#define MN_SIZE (M_SIZE + N_SIZE)
#define MN_BYTES (bitsToBytes(MN_SIZE))
byte MM[MN_BYTES], NN[MN_BYTES];
/* Zero out MM, NN, then copy M, N there (right justified). */
int offset = sizeof(MM) - sizeof(M);
memset (MM, 0, sizeof(MM)); memcpy(MM+offset, M, sizeof(M));
offset = sizeof(NN) - sizeof(N);
memset (NN, 0, sizeof(NN)); memcpy(NN+offset, N, sizeof(N));
dumpBytes(MM, MN_BYTES);
/* Count up "difference bits" until NN has been left-shifted into oblivion. */
int s;
for (s=0; s<MN_SIZE; s++) {
int sum = 0;
for (b=0; b<MN_BYTES; b++) {
int xor = MM[b] ^ NN[b];
while (xor != 0) {
sum += (xor & 1);
xor >>= 1;
}
}
dumpBytes(NN, MN_BYTES);
printf("Shift: %4d; bits: %3d.\n", s, sum);
/* shift NN one bit to the left */
for (b=0; b<MN_BYTES; b++) {
NN[b] <<= 1;
if (b < (MN_BYTES - 1) && ((NN[b+1] & 0x80) != 0)) NN[b] |= 1;
}
}
}