View previous topic :: View next topic |
Author |
Message |
theasus
Joined: 31 May 2009 Posts: 79
|
How can I divide a 16 bit value into 8 bit values? |
Posted: Sat Dec 04, 2010 12:36 am |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
RayJones
Joined: 20 Aug 2008 Posts: 30 Location: Melbourne, Australia
|
|
Posted: Sat Dec 04, 2010 1:37 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
theasus
Joined: 31 May 2009 Posts: 79
|
|
Posted: Sat Dec 04, 2010 2:17 am |
|
|
I couldn't get any result from your link. Could you write a sample about it? |
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sat Dec 04, 2010 2:56 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
theasus
Joined: 31 May 2009 Posts: 79
|
|
Posted: Sat Dec 04, 2010 5:43 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
RayJones
Joined: 20 Aug 2008 Posts: 30 Location: Melbourne, Australia
|
|
Posted: Sat Dec 04, 2010 3:08 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
|