1 //
2 // repack_bytes_example.c
3 //
4 // This example demonstrates the repack_bytes() interface by packing a
5 // sequence of three 3-bit symbols into five 2-bit symbols. The results
6 // are printed to the screen. Because the total number of bits in the
7 // input is 9 and not evenly divisible by 2, the last of the 5 output
8 // symbols has a zero explicitly padded to the end.
9 //
10
11 #include <stdio.h>
12
13 #include "liquid.h"
14
15 // print symbol to screen, one bit at a time
print_symbol(unsigned char _sym,unsigned int _bits_per_symbol)16 void print_symbol(unsigned char _sym,
17 unsigned int _bits_per_symbol)
18 {
19 unsigned int i;
20 unsigned int n; // shift amount
21 for (i=0; i<_bits_per_symbol; i++) {
22 n = _bits_per_symbol - i - 1;
23 printf("%c", (_sym >> n) & 0x01 ? '1' : '0');
24 }
25 }
26
27 // print symbol array to screen
print_symbol_array(unsigned char * _sym,unsigned int _bits_per_symbol,unsigned int _num_symbols)28 void print_symbol_array(unsigned char * _sym,
29 unsigned int _bits_per_symbol,
30 unsigned int _num_symbols)
31 {
32 unsigned int i;
33 for (i=0; i<_num_symbols; i++) {
34 print_symbol(_sym[i], _bits_per_symbol);
35 printf("%c", i < _num_symbols-1 ? ',' : '\n');
36 }
37 }
38
main()39 int main() {
40 // input symbols: 111 000 111
41 // expected output: 11 10 00 11 1(0)
42 unsigned char input[3] = {
43 0x07, // 111
44 0x00, // 000
45 0x07 // 111(0)
46 };
47
48 // allocate memory for output array
49 unsigned char output[5];
50 unsigned int N;
51
52 // print input symbol array
53 printf("input symbols: ");
54 print_symbol_array(input,3,3);
55
56 // repack bytes into output array
57 liquid_repack_bytes( input, 3, 3, output, 2, 5, &N );
58
59 // print output array
60 printf("output symbols: ");
61 print_symbol_array(output,2,5);
62
63 return 0;
64 }
65
66