Velocity Reviews > Code understanding problem

# Code understanding problem

yezi
Guest
Posts: n/a

 11-30-2005
HI all:

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"

Walter Roberson
Guest
Posts: n/a

 11-30-2005
In article <(E-Mail Removed). com>,
yezi <(E-Mail Removed)> 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

Niklas Norrthon
Guest
Posts: n/a

 12-01-2005
"yezi" <(E-Mail Removed)> writes:

> HI all:
>
> 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);
> }

First look up the operators <<, >>=, <<=, &, and |=.

Then start by looking what the values of i and j will
be each iteration:

i = (unsigned long)1<<(bitnum-1) means:
Take 1 (one), and shift it left (bitnum - 1) steps. If bitnum = 8
it is 1 << 7 which in binary is: 1000 0000. j starts at 1 (one), or
in binary: 0000 0001

After each iteration i is shifted right one step, and j shifted left
one step. So i and j will take the following values for bitnum = 8:

i | j
----------+----------
1000 0000 | 0000 0001
0100 0000 | 0000 0010
0010 0000 | 0000 0100
0001 0000 | 0000 1000
0000 1000 | 0001 0000
0000 0100 | 0010 0000
0000 0010 | 0100 0000
0000 0001 | 1000 0000

In the body of the loop there is a test if the i bit of the crc
is set, and if so set the j bit of the return value.

Personally I'd make bitnum unsigned too, check that the bitnum
value us reasonable, skip the cast, init both i and j in the
for statement, and also shift both i and j there.

/Niklas Norrthon