In article <11*********************@o13g2000cwo.googlegroups. com>,
yezi <ye*****@hotmail.com> wrote:
I can not understand the following code , could explain to me in some
easy way:
unsigned long reflect (unsigned long crc, int bitnum) {
// reflects the lower 'bitnum' bits of 'crc'
unsigned long i, j=1, crcout=0;
for (i=(unsigned long)1<<(bitnum-1); i; i>>=1) {
if (crc & i) crcout|=j;
j<<= 1;
}
return (crcout);
}
is the code provide the function - changeing the "11100011" to
"11000111"
The part about 'crc' is irrelevant.
The routine is given an input number, and the number of bits to operate on.
It produces an output which is the lower (least signifcant
value) that-many bits of the input number, but in reverse order.
For example, if bitnum were 4, and the input were binary ...XYZW then
it would first test X (1<<3) and if it were set then it would set
the last bit in the output number, which would become ....000X .
In the next iteration, it would test the next bit rightwards, Y, (1<<2)
and if it were set then it would set the second-last bit in the
output number (1<<1), making the output ...00YX. Next iteration,
next bit rightwards, Z, (1<<1), sets third-last bit (1<<2) in the
output number, getting ...0ZYX. The last iteration, it would test
the last input bit (1<<0) and appropriately set the fourth-last
bit (1<<3) in the output number, getting ...WZYX .
--
Prototypes are supertypes of their clones. -- maplesoft