|
|
View previous topic :: View next topic |
Author |
Message |
stewc Guest
|
Rotate function in c |
Posted: Tue Aug 05, 2008 3:26 am |
|
|
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
|
|
Posted: Tue Aug 05, 2008 3:59 am |
|
|
<<, 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
|
|
Posted: Tue Aug 05, 2008 4:45 am |
|
|
Thanks for your reply
So basically it is eqivalent to
d = d << 1
|
|
|
Ttelmah Guest
|
|
Posted: Tue Aug 05, 2008 6:21 am |
|
|
Exactly.
I thought you didn't understand the usage of <<, rather than the 'shorthand'.
Best Wishes |
|
|
|
|
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
|