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

Reversing the order of bits in a variable

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



Joined: 07 Sep 2010
Posts: 24
Location: West Australia

View user's profile Send private message

Reversing the order of bits in a variable
PostPosted: Mon Oct 18, 2010 10:00 pm     Reply with quote

I'm a few weeks into learning C and think that I'm making a mountain out of a molehill?

The aim is to reverse the order of bits in a variable. I've done it by:
Code:

   int32 x = 0x0; // variable
   int32 y = 0x0; // variable with bits reversed
   int8  z = 0x0; // bit counter
 
    for(z=0;z<=31;z++)
         {
         if(bit_test(x,z)==1) bit_set(y,31-z);
         else bit_clear(y,31-z);
         }


I think there is a better/simpler way to do this. Any advice please.

Regards Bill Legge
_________________
Denmark in West Australia
'Where the forest meets the sea'
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 18, 2010 10:49 pm     Reply with quote

Quote:
The aim is to reverse the order of bits in a variable.

Use the forum's search page. Run it on the Code Library forum.
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