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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
09/06/06 08:29
Read: times


 
#123731 - No, that won't work!
Responding to: ???'s previous message
Sudheer Gaddipati said:
The below code may also be written something as below
if (var & 0x80) 
{ 
   var <<= 1; 
   var != 0x01; 
} 
else 
{ 
   var <<= 1; 
}
My Code:
var <<= 1; 
if (var & 0x80) 
{ 
   var != 0x01; 
}

For a start, you have perpetuated Erik's typo, which he has already corrected:
http://www.8052.com/forum/read.phtml?id=123705

And you've forgotten the 'pre' and '/pre' tags:
http://www.8052.com/forum/read.phtml?id=120199

So the original code should be:
if (var & 0x80) 
{ 
   var <<= 1; 
   var |= 0x01; 
} 
else 
{ 
   var <<= 1; 
}
And your version should be
var <<= 1; 
if (var & 0x80) 
{ 
   var |= 0x01; 
}

Your code does not perform a rotate, As Erik's does!

When you rotate 0x80 (ie, 10000000) you should get 0x01 (ie, 00000001) - the MSB has been rotated into the LSB.

Your code does the shift first, then tests the MSB - but the original MSB will have been lost by the shift!

Anyhow, as Jan observed, the I2C routine requires just a shift - not a rotate!
http://www.8052.com/forum/read.phtml?id=123686

List of 19 messages in thread
TopicAuthorDate
RL & RLC inst in C            01/01/70 00:00      
   yes, they are            01/01/70 00:00      
      yes, they are            01/01/70 00:00      
         mistype            01/01/70 00:00      
      Choose the best tool for the job.            01/01/70 00:00      
      another option (without if)            01/01/70 00:00      
      C style            01/01/70 00:00      
         May Be This Way            01/01/70 00:00      
            No, that won't work!            01/01/70 00:00      
               ways to skin the cat            01/01/70 00:00      
                  Compiler dependant            01/01/70 00:00      
                     compiler independent, but still very ugly version            01/01/70 00:00      
               Thank You            01/01/70 00:00      
         A very important point!            01/01/70 00:00      
      It is nice C            01/01/70 00:00      
         Not necessarily            01/01/70 00:00      
   Problem solved            01/01/70 00:00      
   Keil-specific            01/01/70 00:00      
      Thank you            01/01/70 00:00      

Back to Subject List