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 CCS Technical Support

How can I divide a 16 bit value into 8 bit values?

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



Joined: 31 May 2009
Posts: 79

View user's profile Send private message

How can I divide a 16 bit value into 8 bit values?
PostPosted: Sat Dec 04, 2010 12:36 am     Reply with quote

How can I divide a 16 bit value into 8 bit values?For example;
int16 value;
int8 LSB,MSB;
value=0x4F87;
And I want to change LSB and MSB's values as;
LSB=87;
MSB=4F;
How can I do this?
RayJones



Joined: 20 Aug 2008
Posts: 30
Location: Melbourne, Australia

View user's profile Send private message Visit poster's website

PostPosted: Sat Dec 04, 2010 1:37 am     Reply with quote

My favorite method is to use a structure/union approach.

You can use the inbuilt macro functions to do it, but I simply like the elegance of reading or writing a value directly from/to where it is meant to live.

http://www.ccsinfo.com/forum/viewtopic.php?p=103397&highlight=#103397

Is my "old chestnut" as I like to refer to it :-)

Cheers, Ray
theasus



Joined: 31 May 2009
Posts: 79

View user's profile Send private message

PostPosted: Sat Dec 04, 2010 2:17 am     Reply with quote

I couldn't get any result from your link. Could you write a sample about it?
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Dec 04, 2010 2:56 am     Reply with quote

Ray Jones found the said elegance in a union construct. If you have problem to read it from the link,
a C programming text book or the CCS C manual should help as well. Sooner or later, you won't be
able to ignore these sources, there's not always a forum to spoon feed basic programmers knowledge.

Besides the portable generic C constructs for type manipulation, you should also notice the CCS proprietary make8() function.
theasus



Joined: 31 May 2009
Posts: 79

View user's profile Send private message

PostPosted: Sat Dec 04, 2010 5:43 am     Reply with quote

Sorry about that I understood what you mean I solved my problem.

int16 b;
int LSB,MSB;
LSB=make8(b,0);
MSB=make8(b,1);

Thanks for your considerations.
RayJones



Joined: 20 Aug 2008
Posts: 30
Location: Melbourne, Australia

View user's profile Send private message Visit poster's website

PostPosted: Sat Dec 04, 2010 3:08 pm     Reply with quote

theasus wrote:
I couldn't get any result from your link. Could you write a sample about it?


Huh?

There is an example right there in the link.
As suggested by others, learn the language and you will see how it works.

The union is the secret ingredient. When you understand how they work all will be revealed.
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