View previous topic :: View next topic |
Author |
Message |
hayee
Joined: 05 Sep 2007 Posts: 252
|
Shifting the Bits. |
Posted: Sat Jan 26, 2008 12:40 am |
|
|
Hi,
i am using ccs compiler (version 4.020) and pic16f877a/pic16f870.
i have some data in a variable called (int16 x) and suppose x=1100100100110001
now what i want is that shift 1st 8bit to right so that the value in x read like that
x=0011000111001001.
i want to know that how this shifting can be done.can anyone give any example. |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Sat Jan 26, 2008 5:23 am |
|
|
Starting with the following 16 bit value:
x=1100100100110001
and shifting right >> 2 will become in:
x=0011001001001100
NOT in:
x=0011000111001001.
Quote: |
now what i want is that shift 1st 8bit to right so that
|
1st 8 bit doesn't have any meaning and is ambiguous. It is better to refer as
Most Significant Byte (MSB) or Last Significant Byte (LSB)
Pls tell us exactly what do you need and we will help you.
Humberto |
|
|
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Sat Jan 26, 2008 7:38 am |
|
|
hi,
thanks Humberto.
i have solved the problem by using rotate_left command.actually i want to shift the 8 msb bits at the lsb side and for this purpose i used rotate_left command 8 times. |
|
|
Guest
|
|
Posted: Sat Jan 26, 2008 7:50 am |
|
|
As a comment though, if you want to effectively swap the bytes, you can do this very easily, by just using an 8bit 'temporary' variable, and 'make8' to take the LSB into this, and then make16 to put it together again. So:
Code: |
int16 val=0b1100100100110001;
int8 temp;
temp=make8(val,0); //get the LSB
val=make16(temp,make8(val,1)); //move the MSB to LSB
|
This swaps the bytes in just three instructions, versus probably 16 or more usng the rotation.
Best Wishes |
|
|
|