|
|
View previous topic :: View next topic |
Author |
Message |
dave200 Guest
|
rotate bits.... easy for the pro's |
Posted: Tue Aug 19, 2008 3:48 am |
|
|
Guys,
I have the value 0x94 (10010100) but want to shift the bits to make 0x29 (00101001)
would the statement below be correct?
If not how would i go about doing so?
Code: | value = (0x94 >>= 7); |
Thanks
|
|
|
Ttelmah Guest
|
|
Posted: Tue Aug 19, 2008 4:14 am |
|
|
First key thing, is that you need a rotation, not a shift. The 'C' >> operator, is a shift. With a shift, when bits get to the 'end' of the byte/word, they are lost off the end.
CCS, has a 'shift_right', and 'shift_left' instruction.
Second thing is though, that if you are dealing with a single byte, if the shift/rotation goes for more than four bits, it is quicker to go the other way round. So in your case, shift left by one, rather than right by seven.
Third thing though, is I presume the value is in a variable, not a constant as you show. Otherwise much quicker for everyone just to just do the shift yourself...
So (for instance):
Code: |
int8 value;
value = 0x94; //Starting point
shift_left(&value,1,1);
//Now value will be your required answer
|
Best Wishes |
|
|
dave200 Guest
|
|
Posted: Tue Aug 19, 2008 4:23 am |
|
|
Thanks Ttelmah!
One thing i did notice in your reply is that you state i require a rotate not shift, but your example shows "shift_left".
Is that correct or do i want the rotate_left?
Thanks and regards
|
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Aug 19, 2008 5:53 am |
|
|
Ttelmah is assuming that the MSb of value will always be 1.
To rotate 1 bit to the left
int carry;
int value;
value = 0x94;
carry = (value >> 7);
value = (value << 1) + carry;
OR
rotate_left(&value, 1); |
|
|
Ttelmah Guest
|
|
Posted: Tue Aug 19, 2008 7:08 am |
|
|
Yes, I meant to use 'rotate'. Shows how easy it is to make mistakes....
Best Wishes |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Tue Aug 19, 2008 1:15 pm |
|
|
dave200 wrote:
Quote: |
I have the value 0x94 (10010100) but want to shift the bits to make 0x29 (00101001)
|
I understand that what you want is to reverse the order of the bits, not to rotate nor shift them. If this is what you want, in the following thread you will find an interesting discussion regarding how to.
http://www.ccsinfo.com/forum/viewtopic.php?t=23364
regards,
Humberto |
|
|
|
|
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
|