Code understanding problem

Y

yezi

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"

Thanks for any comments
 
W

Walter Roberson

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 .
 
N

Niklas Norrthon

yezi said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top