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 bits.... easy for the pro's

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







rotate bits.... easy for the pro's
PostPosted: Tue Aug 19, 2008 3:48 am     Reply with quote

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
Smile
Ttelmah
Guest







PostPosted: Tue Aug 19, 2008 4:14 am     Reply with quote

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







PostPosted: Tue Aug 19, 2008 4:23 am     Reply with quote

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
Very Happy
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Tue Aug 19, 2008 5:53 am     Reply with quote

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







PostPosted: Tue Aug 19, 2008 7:08 am     Reply with quote

Yes, I meant to use 'rotate'. Shows how easy it is to make mistakes.... Smile

Best Wishes
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Tue Aug 19, 2008 1:15 pm     Reply with quote

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
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