解决方案
To calculate the bit to shift in you don't need to shift the whole array every time since you are only interested in one bit (note the & 1
at the end of the bit =
line from Wikipedia).
The right shift amounts are:
128 - 128 = 0 => byte 0 bit 0
128 - 29 = 99 => byte 12 bit 3
128 - 27 = 101 => byte 12 bit 5
128 - 2 = 126 => byte 15 bit 6
128 - 1 = 127 => byte 15 bit 7
So,
bit = ((A[0] >> 0)
^ (A[12] >> 3)
^ (A[12] >> 5)
^ (A[15] >> 6)
^ (A[15) >> 7)) & 1;
Now, you really need to shift in the bit:
A[0] = (A[0] >> 1) | (A[1] << 7);
A[1] = (A[1] >> 1) | (A[2] << 7);
// and so on, until
A[14] = (A[14] >> 1) | (A[15] << 7);
A[15] = (A[15] >> 1) | (bit << 7);
You can make this a bit more efficient by using uint32_t
or uint64_t
instead of unsigned chars (depending on your processor word size), but the principle is the same.
不隶属于 StackOverflow