| 64 | |
| 65 | template <typename T> |
| 66 | void print_bits ( T val, std::ostream& out ) |
| 67 | { |
| 68 | std::stringstream strs; |
| 69 | T n_bits = sizeof ( val ) * CHAR_BIT; |
| 70 | int cnt; |
| 71 | for ( unsigned i = 0; i < n_bits; ++i ) |
| 72 | { |
| 73 | cnt = i/10; |
| 74 | strs << cnt << " "; |
| 75 | } |
| 76 | strs << std::endl; |
| 77 | for ( unsigned i = 0; i < n_bits; ++i ) |
| 78 | { |
| 79 | cnt = i%10; |
| 80 | strs << cnt << " "; |
| 81 | } |
| 82 | strs << std::endl; |
| 83 | for ( unsigned i = 0; i < n_bits; ++i ) |
| 84 | { |
| 85 | strs << "--"; |
| 86 | } |
| 87 | strs << std::endl; |
| 88 | for ( unsigned i = 0; i < n_bits; ++i ) |
| 89 | { |
| 90 | strs<< !!( val & 1 ) << " "; |
| 91 | val >>= 1; |
| 92 | } |
| 93 | strs << std::endl; |
| 94 | out << strs.str(); |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Binary search in vectors. |