|
|
View previous topic :: View next topic |
Author |
Message |
John Morley
Joined: 09 Aug 2004 Posts: 97
|
Assembling a 'word' from individual bytes w/o MAKE8/16/32 |
Posted: Wed Aug 25, 2010 8:52 am |
|
|
Hi All,
I'd like to assemble a 32 bit command 'word' out of 3 individual 16 bit long integers each having a max value of 1023 (10 bits). The 10 bits in the three long integers are used to build the 32 bit command word (30 bits + 2 extra). I think I can do this with bit shifting and bit-wise ANDing, but I can't seem to make it work correctly.
Code: |
int16 iRedValue;
int16 iGreenValue;
int16 iBlueValue;
int32 iColorCommandWord;
iRedValue = 1023;
iGreenValue = 1023;
iBlueValue = 1023;
iColorCommandWord = ((iRedValue<<6) & (iGreenValue<<6) & (iBlueValue<<6));
fprintf(USB, "%Lu\n\r", iColorCommandWord);
|
I know that the above code does not work (and I understand why), I just thought this code clearly illustrates what I'm trying to do. In this example, I want to end up with a 32 bit word that is all '1's' except for the two least significant bits that are both '0's'.
Thanks,
John _________________ John Morley |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Wed Aug 25, 2010 9:25 am |
|
|
You have 10bit values.
You appear to want to put these into a 32bit word, as:
xxxxxxxxxxyyyyyyyyyyzzzzzzzzzz00
If so, you need to be shifting the first value, by 22bits, not 6, then the second by 12bits, and the third by 2 bits. So you would need:
Code: |
int16 iRedValue;
int16 iGreenValue;
int16 iBlueValue;
int32 iColorCommandWord;
iRedValue = 1023;
iGreenValue = 1023;
iBlueValue = 1023;
iColorCommandWord = (((int32)iRedValue<<22) & ((int32)iGreenValue<<12) & (iBlueValue<<2));
fprintf(USB, "%Lu\n\r", iColorCommandWord);
|
Remember the values are being shifted, before they go into the result, so need to be converted to int32 _first_, otherwise they won't fit (doesn't apply to the last value).
The alternative, is a shift and combine approach:
Code: |
int16 iRedValue;
int16 iGreenValue;
int16 iBlueValue;
int32 iColorCommandWord;
iRedValue = 1023;
iGreenValue = 1023;
iBlueValue = 1023;
iColorCommandWord = iRedValue;
iColorCommandWord<<=10;
iColorCommandWord &=iGreenValue;
iColorCommandWord<<=10;
iColorCommandWord&=iBlueValue;
iColorCommandWord<<=2;
fprintf(USB, "%Lu\n\r", iColorCommandWord);
|
This will actually take less cycles to complete, since there are only a total of 22bits of rotation, rather then 22+12+2.
Best Wishes |
|
|
John Morley
Joined: 09 Aug 2004 Posts: 97
|
|
Posted: Wed Aug 25, 2010 9:34 am |
|
|
Hi Ttelmah,
Ah, of course! Many thanks for the help!!
John _________________ John Morley |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Aug 25, 2010 9:56 am |
|
|
You need to be oring | the values, not anding & |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Wed Aug 25, 2010 2:30 pm |
|
|
Aargh.
Slap me, I just copied what had been used, and missed this - naughty.....
Best Wishes |
|
|
|
|
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
|