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

from long int to 2 int

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



Joined: 11 May 2009
Posts: 22

View user's profile Send private message

from long int to 2 int
PostPosted: Thu May 21, 2009 6:22 am     Reply with quote

As title say, I need to split a long int variable into 2 different int variable after have shifted left the long variable.
I use this code:
Code:
long int data =0x03ff; //bit from 0 to 9 set to 1
unsigned int datah, datal;

datah = data>>2;
datal=data>>10;

unfortunatly this code doesn't work.
Can you help me?
Thanks
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu May 21, 2009 6:42 am     Reply with quote

Starting with 0x03FF your shift and split:
Code:
datah = data>>2;
datal=data>>10;
will result in datah=0xFF and datal=0x00

What effect do you want to achieve?
blo



Joined: 11 May 2009
Posts: 22

View user's profile Send private message

PostPosted: Thu May 21, 2009 6:48 am     Reply with quote

i'm starting with
data= 0000001111111111 bit from 9 to 0 set 1
then i need to shift left data by 2 bit
data= 0000111111111100
now bit from 15 to 8 must be written into datah and bit from 7 to 0 mast be written into datal

datah=00001111
datal =11111100

P.S.
I'm writing this code beacause i'm doing the library for the 10bit DAC ad5315.
If anyone know where i can find a library done tell me please.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu May 21, 2009 7:05 am     Reply with quote

'>>' shifts to the right, not to the left...

Code:
long int data =0x03FF; //bit from 0 to 9 set to 1
unsigned int datah, datal;

// Solution 1: straight forward
datal = (int8)(data << 2);
datah = (int8)(data >> 6);
 
// Solution 2: easier to understand and shorter code
data <<= 2;
datal = make8(data,0);
datah = make8(data,1);
Solution 2 results in shorter code because there are fewer shifts on the int16. The PIC processors are very efficient in int8 shifting, but for an int16 every single bit shift requires two instructions.
blo



Joined: 11 May 2009
Posts: 22

View user's profile Send private message

PostPosted: Thu May 21, 2009 7:21 am     Reply with quote

I don't know why if I use this code:
Code:
long int data = 0000001111111111;
unsigned int datah,datal;

data <<= 2;
datal = make8(data,0);
datah = make8(data,1);

 lcd_cloc(0,0);     //clear lcd
 printf(lcd_o, "datah= %u" datah);
 lcd_loc(1,0);
 printf(lcd_o, "datal= %u" datal);

datah is 73 -->01001001
datal is 36 -->00100100

for the other code the results are the same.

EDIT:
I'm stupid, when I declare data i've forgot to write 0b befor the value.
now it seems work.
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