LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

bits and bytes

 

 

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?

 

 

0 Kudos
Message 1 of 4
(3,557 Views)

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;
}

 

 

--
Martin
Certified CVI Developer
0 Kudos
Message 2 of 4
(3,553 Views)
Doesn anybody know the x86 assembly to inline in C to do 8 bit swap and 2/4
byte swap ? I know x86 has those instructions, but my assembly-fu is rusty,
to say the least...
--
Guillaume Dargaud
http://www.gdargaud.net/
0 Kudos
Message 3 of 4
(3,542 Views)
0 Kudos
Message 4 of 4
(3,522 Views)