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

Highbyte/Lowbyte

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








Highbyte/Lowbyte
PostPosted: Thu Feb 05, 2009 10:50 am     Reply with quote

I have a simple question........How do you take a 16 bit number and split it into its high and low bytes?

ex.

16bitnumber=(16bitnumberhighbyte, 16bitnumberlowbyte)

I have searched the forums and cant find anyting. I have done it for the basic stamp long time ago.

Thanks
Ttelmah
Guest







PostPosted: Thu Feb 05, 2009 11:22 am     Reply with quote

make16

Best Wishes
Guest








PostPosted: Thu Feb 05, 2009 12:57 pm     Reply with quote

I saw that, but that takes two 8 bit numbers and adds them to make a 16 bit number.

I need to take a 16 bit number and make two 8bits out of it.

Thanks
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Thu Feb 05, 2009 1:40 pm     Reply with quote

Code:
int8 var1, var2;
int16 number;


number = 0xAA88;

var1 = number & 0xFF;// stuff lower byte into var1
var2 = number >> 8 & 0xFF;// shift upper byte to lower position and store it into var2


var1 should equal 0x88 and var2 should equal 0xAA.

Ronald
languer



Joined: 09 Jan 2004
Posts: 144
Location: USA

View user's profile Send private message

PostPosted: Thu Feb 05, 2009 1:43 pm     Reply with quote

From help file:
make8(var, offset)
var is a 16 or 32 bit integer.
offset is a byte offset of 0,1,2 or 3.

EX:
Code:
int32 x;
int y;
y = make8(x,3);  // Gets MSB of x
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 05, 2009 1:46 pm     Reply with quote

Quote:
I need to take a 16 bit number and make two 8bits out of it.

Download the manual and look in the make8, make16 and make32
section:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf

Look at this one:
Quote:
make8( )

Syntax: i8 = MAKE8(var, offset)
Parameters: var is a 16 or 32 bit integer.
offset is a byte offset of 0,1,2 or 3.
Returns: An 8 bit integer
Function: Extracts the byte at offset from var.


For most simple things that you want to do, there will be a function.
You have to look in the manual.
Ttelmah
Guest







PostPosted: Thu Feb 05, 2009 4:09 pm     Reply with quote

Your 'text' question, and your program example, are for the opposite things.
Your text question asks how to get the individual bytes from a 16bit nmber. Make8 is the answer for this. But the line of code you post, is the code to assemble two 8bit values to make a 16bit value. Make16 is the function for this....

Best Wishes
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Fri Feb 06, 2009 12:08 pm     Reply with quote

Interesting thing....

I just tried my solution and make8(). They both create the exact same ASM code.

Ronald
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Feb 06, 2009 12:12 pm     Reply with quote

The CCS manual says:
Quote:

make8( ) --
Extracts the byte at offset from var.
Same as: i8 = (((var >> (offset*8)) & 0xff) except it is done with a single byte move.
Guest








PostPosted: Sat Feb 07, 2009 1:07 pm     Reply with quote

Thanks for the help!! Works beautifully.
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