View previous topic :: View next topic |
Author |
Message |
Linuxbuilders
Joined: 20 Mar 2010 Posts: 193 Location: Auckland NZ
|
shifting bits |
Posted: Mon Aug 02, 2010 7:40 am |
|
|
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
|
|
Posted: Mon Aug 02, 2010 7:56 am |
|
|
What values do you get from each ?
How do you monitor those values ? |
|
|
Linuxbuilders
Joined: 20 Mar 2010 Posts: 193 Location: Auckland NZ
|
|
Posted: Mon Aug 02, 2010 8:12 am |
|
|
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
|
|
Posted: Mon Aug 02, 2010 8:24 am |
|
|
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: 19520
|
|
Posted: Mon Aug 02, 2010 9:06 am |
|
|
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
|
|
Posted: Mon Aug 02, 2010 9:32 am |
|
|
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 |
|
|
Linuxbuilders
Joined: 20 Mar 2010 Posts: 193 Location: Auckland NZ
|
|
Posted: Tue Aug 03, 2010 5:15 am |
|
|
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
|
|
Posted: Tue Aug 03, 2010 6:24 am |
|
|
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. |
|
|
|