03-27-2011 12:49 PM
Hi all,
I have few very simple questions.
1- Can anyone please tell me how to reverse the order of 16 bits? for e.g i have 0010 0011 0100 0101
I want this to be 1010 0010 1100 0100.
2- Is there any command on labwindows which splits a word int two bytes?
03-27-2011 05:05 PM
You need to read a book on C programming.
(1) Obvious way:
unsigned short ReverseBits(unsigned short v)
{
int i;
unsigned short result=0;
for (i=0;i<16;i++)
if (v & (1<<i))
result |= 0x8000U >> i;
return result;
}
(1) Slightly less obvious but quicker way:
unsigned short ReverseBits(unsigned short v)
{
v = ((v & 0xaaaaU)>>1) | ((v & 0x5555U)<<1); // Swap adjacent bits
v = ((v & 0xccccU)>>2) | ((v & 0x3333U)<<2); // Swap adjacent bit pairs
v = ((v & 0xf0f0U)>>4) | ((v & 0x0f0fU)<<4); // Swap adjacent nibbles
v = ((v & 0xff00U)>>8) | ((v & 0x00ffU)<<8); // Swap adjacent bytes
return v;
}
(2) Obvious code:
void SplitWord(unsigned short v, unsigned byte *hi, unsigned byte *lo)
{
*hi = (v & 0xff00U) >> 8;
*lo = v & 0x00ffU;
}
03-28-2011 02:40 AM
03-28-2011 02:20 PM