Packing Bits

From GNU Radio
Jump to navigation Jump to search

Template:TutorialNavigation

This tutorial describes how to pack 8 bits into a byte using the Pack K Bits block, and how unpack a byte into 8 bits, using the Unpack K Bits block.

The previous tutorial, Converting Data Types, describes the char or byte data type and how to convert between data types. The next tutorial, Streams and Vectors, describes the differences between streams and vectors and how to use them in flowgraphs.

Packing Bits

Packing bits into a byte is useful in representing binary data (as opposed to digitized RF samples) as well when using the modulator blocks: Constellation Modulator, GFSK Mod and OFDM Transmitter. Create a new flowgraph and add the Random Source block to the workspace:

AddRandomSourceToWorkspace.png


Click on Random Source. It is selected when outlined in aqua:

SelectRandomSourceBlock.png


Press the UP or DOWN keys to cycle through the different data types until the byte data type is selected, denoted by the magenta output port color:

ChangeRandomSourceToByte.png


The random source generates bytes with a minimum value of Minimum up to a maximum value of Maximum-1. In this case, Minimum = 0 and Maximum = 2, so it will create binary 0 and 1. A Pack K Bits block is used to gather, or pack, multiple bits into a single byte to represent larger binary values.

Add the Pack K Bits, Char to Float, Throttle, QT GUI Histogram Sink to the flowgraph and connect them:

PackBitsStartingFlowgraph.png

The Pack K Bits block takes K bits and places them into a byte by filling the LSB first.

For this example K=4. The Random Source will generate bit A first. This will be received by Pack K Bits and then stored in the LSB:

[0 0 0 A]

The second bit generated by Pack K Bits is B, which is then stored by Pack K Bits according to:

[0 0 B A]

Following this trent, the 3rd and 4th bits C and D will then be stored as:

[D C B A]

The Pack K Bits will then produce the output byte DCBA.

Unpacking Bits

  • pack and unpack bits
  • describe how it works for char/byte type



The next tutorial, Streams and Vectors, describes the differences between streams and vectors and how to use them in flowgraphs.