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

Simple Casting

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



Joined: 24 Jul 2012
Posts: 163

View user's profile Send private message

Simple Casting
PostPosted: Mon Apr 29, 2019 4:01 pm     Reply with quote

So simple I am embarrassed to ask.

Code:
pretrigger_value = 1234;
   MSB = (int)(pretrigger_value>>8);       // MSB
    LSB = (int)pretrigger_value;            // LSB


runs but does not give correct numbers. MSB = 4 and LSB -46 are what I get.

pretrigger_value is defined as INT16.

MSB and LSB are INT8.

What is the correct way to get the MSB and LSB from and INT16? and is there a more efficient way to do it than the >> operator?

CCS 5.064
Windows 10
running 18F87K22 at 60MHz
newguy



Joined: 24 Jun 2004
Posts: 1909

View user's profile Send private message

PostPosted: Mon Apr 29, 2019 4:03 pm     Reply with quote

Look at make8()
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Simple Casting
PostPosted: Mon Apr 29, 2019 4:37 pm     Reply with quote

beaker404 wrote:
So simple I am embarrassed to ask.

Code:
pretrigger_value = 1234;
   MSB = (int)(pretrigger_value>>8);       // MSB
    LSB = (int)pretrigger_value;            // LSB


runs but does not give correct numbers. MSB = 4 and LSB -46 are what I get.

pretrigger_value is defined as INT16.

MSB and LSB are INT8.

It's all in your interpretation. You like to use signed decimal printouts,
but you're really dealing with the hexadecimal world. If you stay in hex,
most of your problems will go bye-bye.

1234 is 0x4D2 (using Windows 7 Prof. calculator in Programmer mode).

So 0x0D2 is the lsb. So select Hex and Byte (two different radio buttons
in Windows 7 calculator). Then type in D2. Then select Decimal and it now displays: -46
So -46 is correct.
beaker404



Joined: 24 Jul 2012
Posts: 163

View user's profile Send private message

PostPosted: Mon Apr 29, 2019 9:12 pm     Reply with quote

Thanks, I knew I was just missing the obvious. I should not code during hay fever season.
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