KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
l_debug.h
Go to the documentation of this file.
1 /* $Id: l_debug.h 14544 2013-05-29 20:34:46Z predoehl $ */
2 /* {{{=========================================================================== *
3  |
4  | Copyright (c) 1994-2010 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Kyle Simek
18  * =========================================================================== }}}*/
19 
20 // vim: tabstop=4 shiftwidth=4 foldmethod=marker
21 
22 #ifndef KJB_CPP_L_DEBUG_H
23 #define KJB_CPP_L_DEBUG_H
24 
25 #include <string>
26 
27 namespace kjb
28 {
29 
30 namespace debug
31 {
32 
42 template <class T>
43 std::string to_bitstring(T v)
44 {
45 #ifdef PRODUCTION
46 #if PRODUCTION == 1
47  std::cerr << "WARNING: executing debugging code in production mode: to_bitstring(). Client code should disable this for production mode.";
48 #endif
49 #endif
50  const int LENGTH = (sizeof(T)) * 9 + 1;
51  char out[LENGTH];
52 
53  unsigned char* bytes = (unsigned char*) &v;
54 
55  // by = byte
56  // bi = bit
57  //
58  int i = 0;
59  for(int by = sizeof(T) - 1; by >= 0; by--)
60  {
61  for(int bi = 0; bi < 8; bi++)
62  {
63  out[i] = (bytes[by] & (1 << bi) ? '1' : '0');
64  i++;
65  }
66 
67  out[i] = ' ';
68  i++;
69 
70  }
71 
72  out[LENGTH - 1] = '\0';
73 
74  return std::string(out);
75 }
76 
77 } // namespace debug
78 } // namespace kjb
79 
80 #endif
std::string to_bitstring(T v)
Definition: l_debug.h:43
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48