How does the cycle index control the loop

Discussion in 'C Programming' started by fl, May 7, 2011.

  1. fl

    fl Guest

    Hi,

    I do not understand the index variable 'j' function below. How deos j
    control to end of the for loop?
    It cycles 8 times from step tracking. When does j equal 0, it ends the
    loop. I do not understand why it ends when j==0.

    Thanks in advance



    ..............
    for (j=0x80; j; j>>=1) {
    bit = crc & crchighbit;
    crc<<= 1;
    if (c & j)
    crc|= 1;
    if (bit)
    crc^= polynom;
    }
     
    fl, May 7, 2011
    #1
    1. Advertising

  2. fl

    fl Guest

    On 7 mai, 12:37, fl <> wrote:
    > Hi,
    >
    > I do not understand the index variable 'j' function below. How deos j
    > control to end of the for loop?
    > It cycles 8 times from step tracking. When does j equal 0, it ends the
    > loop. I do not understand why it ends when j==0.
    >
    > Thanks in advance
    >
    > .............
    > for (j=0x80; j; j>>=1) {
    >  bit = crc & crchighbit;
    >  crc<<= 1;
    >  if (c & j)
    >     crc|= 1;
    >  if (bit)
    >     crc^= polynom;
    >
    >
    >
    > }- Masquer le texte des messages précédents -
    >
    > - Afficher le texte des messages précédents -


    The second item 'j' in the for loop means it is non-zero. This non-
    zero as the termination criteria? Thanks.
     
    fl, May 7, 2011
    #2
    1. Advertising

  3. fl

    Lew Pitcher Guest

    On May 7, 2011 12:37, in comp.lang.c, wrote:

    > Hi,
    >
    > I do not understand the index variable 'j' function below. How deos j
    > control to end of the for loop?
    > It cycles 8 times from step tracking. When does j equal 0, it ends the
    > loop. I do not understand why it ends when j==0.
    >
    > Thanks in advance


    You can read the statement
    for (j=0x80; j; j>>=1) { /* stuff */ }

    as
    initialize j to 0x80
    while j is not zero

    do some stuff

    drop the low-order bit of j, shift the remaining bits right by 1 bit,
    and fill in the vacated high order bit with a 0

    go back to the top of the loop.


    If you walk the loop through:
    j is set to 0x80, j is not zero, the loop body is run, and j becomes 0x40
    j is 0x40, j is not zero, the loop body is run, and j becomes 0x20
    j is 0x20, j is not zero, the loop body is run, and j becomes 0x10
    j is 0x10, j is not zero, the loop body is run, and j becomes 0x08
    j is 0x08, j is not zero, the loop body is run, and j becomes 0x04
    j is 0x04, j is not zero, the loop body is run, and j becomes 0x02
    j is 0x02, j is not zero, the loop body is run, and j becomes 0x01
    j is 0x01, j is not zero, the loop body is run, and j becomes 0x00
    j is 0x00, j is zero, the loop terminates

    That's 8 times that j is not zero, and the loop body is executed.

    HTH
    --
    Lew Pitcher
    Master Codewright & JOAT-in-training | Registered Linux User #112576
    Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
    ---------- Slackware - Because I know what I'm doing. ------
     
    Lew Pitcher, May 7, 2011
    #3
  4. On May 7, 6:45 pm, fl <> wrote:
    > On 7 mai, 12:37, fl <> wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > Hi,

    >
    > > I do not understand the index variable 'j' function below. How deos j
    > > control to end of the for loop?
    > > It cycles 8 times from step tracking. When does j equal 0, it ends the
    > > loop. I do not understand why it ends when j==0.

    >
    > > Thanks in advance

    >
    > > .............
    > > for (j=0x80; j; j>>=1) {
    > >  bit = crc & crchighbit;
    > >  crc<<= 1;
    > >  if (c & j)
    > >     crc|= 1;
    > >  if (bit)
    > >     crc^= polynom;

    >
    > > }- Masquer le texte des messages précédents -

    >
    > > - Afficher le texte des messages précédents -

    >
    > The second item 'j' in the for loop means it is non-zero. This non-
    > zero as the termination criteria? Thanks.


    j <-- 0b10000000
    While j is not 0 (any non-zero value i interpreted as true)
    Shift j one position to the right.

    The single one will then be shifted out after 8 iterations, resulting
    in j=0 (i.e. false) and the loop stops.
     
    Kleuskes & Moos, May 7, 2011
    #4
  5. fl <> writes:
    > I do not understand the index variable 'j' function below. How deos j
    > control to end of the for loop?
    > It cycles 8 times from step tracking. When does j equal 0, it ends the
    > loop. I do not understand why it ends when j==0.
    >
    > .............
    > for (j=0x80; j; j>>=1) {
    > bit = crc & crchighbit;
    > crc<<= 1;
    > if (c & j)
    > crc|= 1;
    > if (bit)
    > crc^= polynom;
    > }


    Whenever an expression (say, "foo") appears in a context that requires a
    condition (if, while, do-while, the second clause of a for loop), the
    result of the expression is compared to zero, and the condition is true
    if the expression is unequal to zero.

    For example:

    int foo = 0;
    if (foo) puts("This will not be printed");
    foo = 1;
    if (foo) puts("This will be printed");
    foo = 42;
    if (foo) puts("This will also be printed");

    Prior to the C99 standard, C did not have a dedicated boolean type.
    Instead, any scalar (even a pointer) could be used as a condition,
    with 0 meaning false and non-0 meaning true. C99 added _Bool
    (and bool, if you #include <stdbool.h>), but it was merely tacked
    onto the existing language; the use of arbitrary scalar values as
    conditions is unchanged.

    See section 9 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, May 7, 2011
    #5
    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. wanwan

    Thread loop cycle time

    wanwan, Apr 26, 2006, in forum: Java
    Replies:
    6
    Views:
    621
    Roedy Green
    Apr 26, 2006
  2. bogdan
    Replies:
    1
    Views:
    2,418
    Teemu Keiski
    Mar 20, 2008
  3. dmitrey

    "for" cycle with assigning index

    dmitrey, Aug 15, 2009, in forum: Python
    Replies:
    5
    Views:
    447
    dmitrey
    Aug 16, 2009
  4. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    317
    Tomasz Chmielewski
    Mar 4, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    391
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page