Code understanding problem

Discussion in 'C Programming' started by yezi, Nov 30, 2005.

  1. yezi

    yezi Guest

    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
     
    yezi, Nov 30, 2005
    #1
    1. Advertising

  2. In article <>,
    yezi <> 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
     
    Walter Roberson, Nov 30, 2005
    #2
    1. Advertising

  3. "yezi" <> 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
     
    Niklas Norrthon, Dec 1, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JM
    Replies:
    3
    Views:
    2,158
    Andy Dingley
    Oct 1, 2005
  2. Dhruva Hein

    Help understanding code

    Dhruva Hein, Apr 10, 2005, in forum: Python
    Replies:
    2
    Views:
    325
    Bruno Desthuilliers
    Apr 11, 2005
  3. Fredrik Lundh

    Re: Help understanding code

    Fredrik Lundh, Apr 10, 2005, in forum: Python
    Replies:
    1
    Views:
    333
    Duncan Booth
    Apr 11, 2005
  4. Thomas Ploch
    Replies:
    0
    Views:
    338
    Thomas Ploch
    Nov 17, 2006
  5. Andreas

    problem understanding c code

    Andreas, Nov 14, 2007, in forum: C Programming
    Replies:
    4
    Views:
    327
    Ben Bacarisse
    Nov 14, 2007
Loading...

Share This Page