CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Rotate function in c

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
stewc
Guest







Rotate function in c
PostPosted: Tue Aug 05, 2008 3:26 am     Reply with quote

Hi,

This may be a bit off topic but I have seen this bit of code from an I2C function and I dont understand what it does.

Code:
for(x=0; x<8; x++) {
    d <<= 1;
    do {
      SCL = 1;
    }


I dont understand the syntax of the "<<="

Thanks
Ttelmah
Guest







PostPosted: Tue Aug 05, 2008 3:59 am     Reply with quote

<<, is a 'standard' C operator. Just like +,-, * etc..
It is one of the family of 'bitwise' operators available in C. It is a _shift_ (not a 'rotate' as in your question), and shifts the left hand operand left, the number of bits specified in the right hand operand, filling the vacated bits with zeros. So:
Code:

int8 a;
a=2;

a = a<<2;

Will put back into 'a', it's own value left shifted twice.
Lot's of caveats, since you need to know the bit order on the processor involved. On the PIC, left shifting, is equivalent to multiplying by 2, so shifting twice, gives the equivalent to multiplying by 4. So 'a' ends up as 8.
Now, in your example, it has been used in the C 'operand=' construct, which allows you to leave out the left hand operand, and the command shifts by 1.
There is also a >> for right shifting.
It is standard C, and any text book should give details.

Best Wishes
stewc
Guest







PostPosted: Tue Aug 05, 2008 4:45 am     Reply with quote

Thanks for your reply

So basically it is eqivalent to

d = d << 1

Very Happy
Ttelmah
Guest







PostPosted: Tue Aug 05, 2008 6:21 am     Reply with quote

Exactly.
I thought you didn't understand the usage of <<, rather than the 'shorthand'.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group