Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
10/21/10 07:40
Read: times


 
Msg Score: +1
 +1 Informative
#179232 - Quite common loop design for bit operations
Responding to: ???'s previous message
Justin Fontes said:
Plus I have never seen a for loop written in this manner:

for (i = 0x01; i; i <<= 1)

i is instantiated as 0x01, the for loop never compares or just makes sure i is 1? isn't supposed to be "i < 0x80" in the middle?

Nothing strange with that loop. It's a one-bit-at-a-time loop, that is quite common if either writing a running light (walk the LED from D0 to D7) or when counting number of bits non-zero in a variable.

If i is an 8-bit variable, it will iterate the values 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 and break when that lonely bit gets shifted out into the void. With a 16-bit i, it will iterate 16 times instead.

Remember that C don't need a comparison operator for logic expressions. It's just a question if the expression is zero or non-zero.

so:
char *p = NULL;
int i = 0;
if (!p) {
    printf("was NULL pointern");
}
if (p) {
    printf("was non-NULL pointern");
}
if (i) {
    printf("was non-zero integern");
}
if (!i) {
    printf("was zero integern");
}

That's also why you can write code like:
int a = 5;
int b = 10;
int same = a == b;

A comparison in C isn't a magic datatype "bool" - it's just a numeric expression that is zero if the comparison is false. And since there isn't a forced bool data type, if statements don't require any comparison at all - just an expression that is zero or non-zero.

List of 49 messages in thread
TopicAuthorDate
p89lpc936 keil programming help required.            01/01/70 00:00      
   magic code?            01/01/70 00:00      
      mov 0A5H,#0FFH            01/01/70 00:00      
   a glaring difference            01/01/70 00:00      
      a glaring difference            01/01/70 00:00      
         what happens if            01/01/70 00:00      
            what happens if            01/01/70 00:00      
               BiDir or PushPull            01/01/70 00:00      
               I leave it to you            01/01/70 00:00      
                  magic code?            01/01/70 00:00      
                  I leave it to you            01/01/70 00:00      
                     1) formatted, 2)commented, 3) correct            01/01/70 00:00      
                        1) formatted, 2)commented, 3) correct            01/01/70 00:00      
                           it is STILL two different things            01/01/70 00:00      
                              Apples and Orange Juice            01/01/70 00:00      
                                 Please focus on the problem i have            01/01/70 00:00      
                                    Software delay loop in C is a no-no            01/01/70 00:00      
                                       Software delay loop in C is a no-no            01/01/70 00:00      
                                          Reduce problem into smaller problems            01/01/70 00:00      
                                             Reduce problem into smaller problems            01/01/70 00:00      
                                             Simulator vs real hardware            01/01/70 00:00      
                                             Current limits            01/01/70 00:00      
                                             Per Westermark's previous message            01/01/70 00:00      
                                                Delay speed            01/01/70 00:00      
                                                   Elaborate            01/01/70 00:00      
                                                      tried 5 for the '51 and Keil won            01/01/70 00:00      
                                    the problem you have is ...            01/01/70 00:00      
                                       the problem you have is ...            01/01/70 00:00      
                                          do you have LEDs connected to the simulator?            01/01/70 00:00      
                                          not executing on actual hardware while simulator is fine            01/01/70 00:00      
                                    Oh So Focused            01/01/70 00:00      
   Double-post            01/01/70 00:00      
   Are you using the limited version of KEIL? the for loop....            01/01/70 00:00      
      valid C            01/01/70 00:00      
         Thats the point            01/01/70 00:00      
            nope            01/01/70 00:00      
               I wouldnt trust it            01/01/70 00:00      
                  if you do not trust it ....            01/01/70 00:00      
               And if you trust it so much why doesnt it work?            01/01/70 00:00      
                  And if you trust it so much why doesnt it work?            01/01/70 00:00      
                     trust, yes, but knowledge also required            01/01/70 00:00      
                  wait call + assumptions about two-complement, ...            01/01/70 00:00      
                     The C Standard            01/01/70 00:00      
                        who is "you"?            01/01/70 00:00      
            Yes, you can!            01/01/70 00:00      
      Quite common loop design for bit operations            01/01/70 00:00      
      for( expression-1; expression-2; expression-3 ) [ed]            01/01/70 00:00      
         erratum            01/01/70 00:00      
            Corrigendum            01/01/70 00:00      

Back to Subject List