LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Packing and unpacking bits

Solved!
Go to solution

Hi Billko,

It is some thing similar but more in line with my previous post.

0 Kudos
Message 11 of 22
(1,775 Views)
Solution
Accepted by topic author SolPS

@SolPS wrote:

I can only provide a description as shown below

Byte     no of Bits     Data       Value         Data Type

 0             2              Data1      0-2            Enumeration


Looks like we cross-posted.  That's perfect.  So it looks like you're sending one byte.  The data itself is a two bit number 0-2, or 00, 01, 10 binary.

bit stuffing.png

Oh, Data1 is an 8-bit enum.  Enum prevents you from entering invalid data.  Guess it would've been helpful if I took the screenie after I ran it.  Sorry.

 

Also, some people like to use Boolean arrays; I like to use clusters and convert them into an array so there is no way you can end up with too many/not enough elements.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 12 of 22
(1,771 Views)

You won't be able to represent your enum with less than 8 bits.  You'll just have to live with it and make it impossible for the user to select invalid values like how I did in the snippet.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 13 of 22
(1,765 Views)

Hi Bilko,

I'm sending a combination of data types in one byte.  More like the description below. 

Byte     no of Bits     Data       Value                   Data Type

 0             2              Data1      0-2                      Enumeration

                1              Data2       1/0                     1bit Boolean         

                1              Data3       1/0                     1bit Boolean

                1              Data4       1/0                     1bit Boolean

                3              Data5        0-2                    Enumeration

 

                

0 Kudos
Message 14 of 22
(1,760 Views)

You should be able to expand my example to include your revised format.  🙂

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 15 of 22
(1,758 Views)

Ok. I will give it a try. Thanks for your replies.

0 Kudos
Message 16 of 22
(1,754 Views)

@SolPS wrote:

Hi Bilko,

I'm sending a combination of data types in one byte.  More like the description below. 

Byte     no of Bits     Data       Value                   Data Type

 0             2              Data1      0-2                      Enumeration

                1              Data2       1/0                     1bit Boolean         

                1              Data3       1/0                     1bit Boolean

                1              Data4       1/0                     1bit Boolean

                3              Data5        0-2                    Enumeration

 

                


That last value (3bits) can be from 0..7, of course, but you don't really need to use all bits.

It is extremely wasteful to explicitly code all these shifts and masks, because there are so many places for bugs to hide. Use a FOR loop and you are all set. All you need to know is the number of bits per field. Right?

 

Here's a simple draft:

 

altenbach_0-1588704590373.png

 

 

Of course you don't really save anything useful. Sending one byte vs. five bytes over UDP costs you a packet either way and the size difference in payload is negligible compared to the entire packet with headers, etc. You could just cast of flatten the cluster to a string and back.

 

0 Kudos
Message 17 of 22
(1,746 Views)

Hi Altenbach,

I need to send the entire byte. The last 3 bits will be used in padding the data.

Thanks.

0 Kudos
Message 18 of 22
(1,741 Views)

@SolPS wrote:

Hi Altenbach,

I need to send the entire byte. The last 3 bits will be used in padding the data.


Since the minimum size is on byte, there is no padding. You re simply ignoring these bits. 😉

0 Kudos
Message 19 of 22
(1,738 Views)

@altenbach wrote:

@SolPS wrote:

Hi Bilko,

I'm sending a combination of data types in one byte.  More like the description below. 

Byte     no of Bits     Data       Value                   Data Type

 0             2              Data1      0-2                      Enumeration

                1              Data2       1/0                     1bit Boolean         

                1              Data3       1/0                     1bit Boolean

                1              Data4       1/0                     1bit Boolean

                3              Data5        0-2                    Enumeration

 

                


That last value (3bits) can be from 0..7, of course, but you don't really need to use all bits.

It is extremely wasteful to explicitly code all these shifts and masks, because there are so many places for bugs to hide. Use a FOR loop and you are all set. All you need to know is the number of bits per field. Right?

 

Here's a simple draft:

 

altenbach_0-1588704590373.png

 

 

Of course you don't really save anything useful. Sending one byte vs. five bytes over UDP costs you a packet either way and the size difference in payload is negligible compared to the entire packet with headers, etc. You could just cast of flatten the cluster to a string and back.

 


I don't think that is correct.  It seems to me that the formatting of the data has already been set.  It is one byte, consisting of: one number represented by two bits, three Booleans, then another number represented by three bits.  I think there is no room for interpretation here.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 20 of 22
(1,723 Views)