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

shifting bits

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



Joined: 20 Mar 2010
Posts: 193
Location: Auckland NZ

View user's profile Send private message

shifting bits
PostPosted: Mon Aug 02, 2010 7:40 am     Reply with quote

Hi,
is there anyone who could explain to me why this is not the some thing?
PCH 4.107

data1dec_h = data1dec/0x100;
data1dec_l = (data1dec-(data1dec_h*0x100));

above works

data1dec_h = data1dec>>8;
data1dec_l = (data1dec-(data1dec_h<<8));

above does not work

??

thnx
_________________
Help "d" others and then you shell receive some help from "d" others.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Aug 02, 2010 7:56 am     Reply with quote

What values do you get from each ?

How do you monitor those values ?
Linuxbuilders



Joined: 20 Mar 2010
Posts: 193
Location: Auckland NZ

View user's profile Send private message

PostPosted: Mon Aug 02, 2010 8:12 am     Reply with quote

int8 each joined into one int16, it does not compute to the some value, will post some examples tomorrow. Thnx
_________________
Help "d" others and then you shell receive some help from "d" others.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Aug 02, 2010 8:24 am     Reply with quote

If your intention is to split a 16 bit value in to 2 8 bit values then the 2 options you have presented are not the best. If you are lucky the compiler will optimise it but the standard C way would be :-

Code:

int16 data1dec;
int8 data1dec_h, data1dec_l;

data1dec_h = data1dec >> 8;
data1dec_l = data1dec;  // Should automatically take the lower 8 bits

Or to be more specific

data1dec_h = data1dec >> 8;
data1dec_l = data1dec & 0xFF;

Or to use CCS which most likely will provide the most efficient way

data1dec_h = make8(data1dec, 1);
data1dec_l = make8(data1dec, 0);
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Mon Aug 02, 2010 9:06 am     Reply with quote

As for why it doesn't work, sizes....

If you take a 8bit value, and shift it left 8bits, you have nothing. It is an eight bit value, and everything disappears out the 'top'.
However if you take an 8bit value, and multiply it by 0x100, you get the byte in the top 8bits of a sixteen bit value.
The 'reason', is that 0x100, is inherently a 16bit integer. Hence when this is seen, the arithmetic is immediately switched to using 16bit operations.

Best Wishes
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Aug 02, 2010 9:32 am     Reply with quote

Ttelmah wrote:
As for why it doesn't work, sizes....

If you take a 8bit value, and shift it left 8bits, you have nothing. It is an eight bit value, and everything disappears out the 'top'.
However if you take an 8bit value, and multiply it by 0x100, you get the byte in the top 8bits of a sixteen bit value.
The 'reason', is that 0x100, is inherently a 16bit integer. Hence when this is seen, the arithmetic is immediately switched to using 16bit operations.

Best Wishes


Well spotted Smile
Linuxbuilders



Joined: 20 Mar 2010
Posts: 193
Location: Auckland NZ

View user's profile Send private message

PostPosted: Tue Aug 03, 2010 5:15 am     Reply with quote

cool bananas, thank you, now I understand...

by the way I did say wrong, I split one int16 value into 2x int8.

so it is like this:

int16 x;
int8 x_h, x_l;

x_h = x/0x100;
x_l = x-(x_h*0x100);
_________________
Help "d" others and then you shell receive some help from "d" others.
Linuxbuilders



Joined: 20 Mar 2010
Posts: 193
Location: Auckland NZ

View user's profile Send private message

PostPosted: Tue Aug 03, 2010 6:24 am     Reply with quote

Linuxbuilders wrote:
cool bananas, thank you, now I understand...

by the way I did say wrong, I split one int16 value into 2x int8.

so it is like this:

int16 x;
int8 x_h, x_l;

x_h = x/0x100;
x_l = x-(x_h*0x100);


This works:

int b, c, d;
long int a, aa, bb;

a = 0xAA;
b = 0xBB;

bb = a<<8;
aa = (bb)+b;

c = aa>>8;
d = aa;
_________________
Help "d" others and then you shell receive some help from "d" others.
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