Houston, we have a trunk.
[debian/gnuradio] / gr-error-correcting-codes / src / lib / libecc / mld / n2bs.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio.
6  *
7  * Primary Author: Michael Dickens, NCIP Lab, University of Notre Dame
8  * 
9  * GNU Radio is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2, or (at your option)
12  * any later version.
13  * 
14  * GNU Radio is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  * 
19  * You should have received a copy of the GNU General Public License
20  * along with GNU Radio; see the file COPYING.  If not, write to
21  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include <n2bs.h>
26 #include <iostream>
27
28 const int g_num_bits_per_byte = 8;
29
30 std::string n2bs (long long number, size_t digits)
31 {
32   if (digits > (sizeof (long long) * g_num_bits_per_byte))
33     digits = sizeof (long long);
34   std::string retVal (digits, '0');
35   if (number != 0)
36     for (int n = --digits; n >= 0; n--) {
37       if (number & 1) {
38         retVal[n] = '1';
39       }
40       number >>= 1;
41     }
42   return (retVal);
43 }
44 std::string n2bs (char number, size_t digits)
45 {
46   if (digits > (sizeof (char) * g_num_bits_per_byte))
47     digits = sizeof (char);
48   return n2bs ((long long) number, digits);
49 }
50 std::string n2bs (int number, size_t digits)
51 {
52   if (digits > (sizeof (int) * g_num_bits_per_byte))
53     digits = sizeof (int);
54   return n2bs ((long long) number, digits);
55 }
56 std::string n2bs (long number, size_t digits)
57 {
58   if (digits > (sizeof (long) * g_num_bits_per_byte))
59     digits = sizeof (long);
60   return n2bs ((long long) number, digits);
61 }
62 std::string n2bs (size_t number, size_t digits)
63 {
64   if (digits > (sizeof (size_t) * g_num_bits_per_byte))
65     digits = sizeof (size_t);
66   return n2bs ((long long) number, digits);
67 }
68
69 void cout_binary (int number, int digits)
70 {
71   while (digits-- > 0)
72     std::cout << ((number >> digits) & 1);
73 }