J
jacob navia
Given a bitstring of size siz (in bytes), shift it right by 1 bit.
Return the bit that was shifted out
static int Shift(char *p,size_t siz)
{
int carry;
size_t i;
carry = p[0]&1;
p[0] >>= 1;
for (i=1; i<siz;i++) {
int s = p&1;
p = (p >> 1) | (carry << 7);
if (i < siz-1)
carry = p[i+1]&1;
else carry = s;
}
return carry;
}
Somehow it doesn't look very well. I am sure that there is a better solution.
Any takers?
jacob
Return the bit that was shifted out
static int Shift(char *p,size_t siz)
{
int carry;
size_t i;
carry = p[0]&1;
p[0] >>= 1;
for (i=1; i<siz;i++) {
int s = p&1;
p = (p >> 1) | (carry << 7);
if (i < siz-1)
carry = p[i+1]&1;
else carry = s;
}
return carry;
}
Somehow it doesn't look very well. I am sure that there is a better solution.
Any takers?
jacob