- - http://168iroha.net 018 10 14
i 1 1 1.1.................................................... 1 1.................................................... 7.1................................................ 7..................................................... 9
1 1 1.1 char 8bit 3 // 4 template <imsize_t N> 5 class bitset { 6 template <imsize_t > friend class bitset; 7 8 static constexpr imsize_t array_size = ((N - 1) >> 3) + 1; 9 unsigned char x[array_size]; 10 public: 11 constexpr bitset(); 1 bitset(const bitset& b); 13 template <imsize_t N> 14 bitset(const bitset <N>&); 15 bitset(imsize_t n); 16 template <class CharT, class Predicate, class Allocator > 17 explicit bitset(const string <CharT, Predicate, Allocator >&); 18 bitset() {} 19 0 // 1 bitset operator () const; // 3 bitset& operator=(const bitset&); 4 bitset& operator&=(const bitset&); 5 bitset& operator =(const bitset&); 6 bitset& operatorˆ=(const bitset&); 7 bitset& operator <<=(imsize_t); 8 bitset& operator >>=(imsize_t); 9 // 30 bool operator==(const bitset&) const; 31 bool operator!=(const bitset&) const; 3 33 constexpr imsize_t size() const noexcept; 34 // 35 unsigned char byte(imsize_t) const; 36 // 37 bool bit(imsize_t) const; 38 bool operator[]( imsize_t) const; 39 40 // 41 bitset& reset(); 4 // 43 bitset& set(); 44 bitset& set(imsize_t, bool flag); 45 // 46 bitset& flip(); 47 // ( ) 48 bitset& flip(imsize_t pos); 49 // 50 imsize_t to_uint() const; 51 unsigned long to_ulong() const; 5 // 53 template <class CharT, class Predicate = type_comparison <CharT >, class Allocator = allocator <CharT,
1 array_iterator <CharT >>> 54 string <CharT, Predicate, Allocator > to_string() const; 55 //1 56 imsize_t count() const; 57 }; 58 } string string 1. 3 template <imsize_t N> 4 class bitset { 5 // N 0 6 bitset& byte_check() { 7 //8 8 if ((N & 7)!= 0) x[array_size - 1] &= (1 << (N & 7)) - 1; 9 return *this; 10 } 11 }; 1 } bitset& 3 template <imsize_t N> 4 class bitset { 5 public: 6 // 7 unsigned char byte(imsize_t n) const { return x[n]; } 8 // 9 bool operator[]( imsize_t pos) const { return (x[pos >> 3] & (1 << (pos & 7))) > 0; } 10 bool bit(imsize_t pos) const { return (x[pos >> 3] & (1 << (pos & 7))) > 0; } 11 }; 1 } true or false and 3 template <imsize_t N> 4 class bitset { 5 public: 6 constexpr bitset() :x{} {} 7 bitset(const bitset& b) { 8 for (imsize_t i = 0; i < array_size; ++i) this ->x[i] = b.x[i]; 9 } 10 template <imsize_t N> 11 bitset(const bitset <N>& b) : x{} { 1 for (imsize_t i = 0, n = (min)(this ->array_size, b.array_size); i < n; ++i) this ->x[i] = b.x[i]; 13 } 14 bitset(imsize_t n) :x{} { 15 // 16 switch (array_size) { 17 case 1: 18 x[0] = static_cast <unsigned char >(n & 0xFF); 19 break; 0 case : 1 x[0] = static_cast <unsigned char >(n & 0xFF);
1 3 x[1] = static_cast <unsigned char >((n & (0xFF << 8)) >> 8); 3 break; 4 case 3: 5 x[0] = static_cast <unsigned char >(n & 0xFF); 6 x[1] = static_cast <unsigned char >((n & (0xFF << 8)) >> 8); 7 x[] = static_cast <unsigned char >((n & (0xFF << 16)) >> 16); 8 break; 9 //64 b i t 30 #if defined _IMATH_INT_64BIT_ 31 case 4: 3 x[0] = static_cast <unsigned char >(n & 0xFF); 33 x[1] = static_cast <unsigned char >((n & (0xFF << 8)) >> 8); 34 x[] = static_cast <unsigned char >((n & (0xFF << 16)) >> 16); 35 x[3] = static_cast <unsigned char >((n & (0xFF << 4)) >> 4); 36 break; 37 case 5: 38 x[0] = static_cast <unsigned char >(n & 0xFF); 39 x[1] = static_cast <unsigned char >((n & (0xFF << 8)) >> 8); 40 x[] = static_cast <unsigned char >((n & (0xFF << 16)) >> 16); 41 x[3] = static_cast <unsigned char >((n & (0xFF << 4)) >> 4); 4 x[4] = static_cast <unsigned char >((n & (0xFF << 3)) >> 3); 43 break; 44 case 6: 45 x[0] = static_cast <unsigned char >(n & 0xFF); 46 x[1] = static_cast <unsigned char >((n & (0xFF << 8)) >> 8); 47 x[] = static_cast <unsigned char >((n & (0xFF << 16)) >> 16); 48 x[3] = static_cast <unsigned char >((n & (0xFF << 4)) >> 4); 49 x[4] = static_cast <unsigned char >((n & (0xFF << 3)) >> 3); 50 x[5] = static_cast <unsigned char >((n & (0xFF << 40)) >> 40); 51 break; 5 case 7: 53 x[0] = static_cast <unsigned char >(n & 0xFF); 54 x[1] = static_cast <unsigned char >((n & (0xFF << 8)) >> 8); 55 x[] = static_cast <unsigned char >((n & (0xFF << 16)) >> 16); 56 x[3] = static_cast <unsigned char >((n & (0xFF << 4)) >> 4); 57 x[4] = static_cast <unsigned char >((n & (0xFF << 3)) >> 3); 58 x[5] = static_cast <unsigned char >((n & (0xFF << 40)) >> 40); 59 x[6] = static_cast <unsigned char >((n & (0xFF << 48)) >> 48); 60 break; 61 #endif 6 default: 63 x[0] = static_cast <unsigned char >(n & 0xFF); 64 x[1] = static_cast <unsigned char >((n & (0xFF << 8)) >> 8); 65 x[] = static_cast <unsigned char >((n & (0xFF << 16)) >> 16); 66 x[3] = static_cast <unsigned char >((n & (0xFF << 4)) >> 4); 67 #if defined _IMATH_INT_64BIT_ 68 x[4] = static_cast <unsigned char >((n & (0xFF << 3)) >> 3); 69 x[5] = static_cast <unsigned char >((n & (0xFF << 40)) >> 40); 70 x[6] = static_cast <unsigned char >((n & (0xFF << 48)) >> 48); 71 x[7] = static_cast <unsigned char >((n & (0xFF << 56)) >> 56); 7 #endif 73 } 74 byte_check(); 75 } 76 template <class CharT, class Predicate, class Allocator > 77 explicit bitset(const string <CharT, Predicate, Allocator >& str) { 78 for (imsize_t i = 0; i < array_size; ++i) { 79 for (imsize_t j = 0; j < 8; ++j) { 80 if (str.size() - - ((i << 3) + j) < 0) x[i] &= ((1 << j) - 1); 81 else if (str[str.size() - - ((i << 3) + j)] == 1 ) x[i] = (1 << j); 8 else if (str[str.size() - - ((i << 3) + j)] == 0 ) x[i] &= (1 << j); 83 } 84 } 85 byte_check(); 86 } 87 bitset() {} 88 89 // 90 bitset operator () const { 91 bitset <N> temp; 9 for (imsize_t i = 0; i < array_size; ++i) temp.x[i] = this ->x[i]; 93 return temp.byte_check(); 94 } 95 // 96 bitset& operator=(const bitset& b) { 97 for (imsize_t i = 0; i < array_size; ++i) this ->x[i] = b.x[i]; 98 } 99 bitset& operator&=(const bitset& b) { 100 for (imsize_t i = 0; i < array_size; ++i) this ->x[i] &= b.x[i]; 101 } 10 bitset& operator =(const bitset& b) { 103 for (imsize_t i = 0; i < array_size; ++i) this ->x[i] = b.x[i]; 104 } 105 bitset& operatorˆ=(const bitset& b) { 106 for (imsize_t i = 0; i < array_size; ++i) this ->x[i] ˆ= b.x[i]; 107 } 108 // 109 bool operator==(const bitset& b) const { 110 for (imsize_t i = 0; i < array_size; ++i) if (this ->x[i]!= b.x[i]) return false;
1 4 111 return true; 11 } 113 bool operator!=(const bitset& b) const { 114 return (*this == b); 115 } 116 117 constexpr imsize_t size() const noexcept { return N; } 118 // 119 unsigned char byte(imsize_t n) const { return x[n]; } 10 11 // 1 bitset& reset() { 13 for (imsize_t i = 0; i < array_size; ++i) x[i] = 0; 14 return *this; 15 } 16 // 17 bitset& set() { 18 for (imsize_t i = 0; i < array_size; ++i) x[i] = 0xFF; 19 return byte_check(); 130 } 131 bitset& set(imsize_t pos, bool flag = true) { 13 if (pos >= N) return *this; 133 if(flag) x[pos >> 3] = (1 << (pos & 7)); 134 else x[pos >> 3] &= (1 << (pos & 7)); 135 return *this; 136 } 137 // 138 bitset& flip() { 139 for (imsize_t i = 0; i < array_size; ++i) this ->x[i] = this ->x[i]; 140 return byte_check(); 141 } 14 // 143 bitset& flip(imsize_t pos) { 144 if (pos >= N) return *this; 145 x[pos >> 3] ˆ= (1 << (pos & 7)); 146 return *this; 147 } 148 149 // 150 imsize_t to_uint() const { 151 imsize_t result = 0; 15 for (imsize_t i = 0, n = (min<imsize_t >)(4, array_size); i < n; ++i) result = x[i] << (8 * i); 153 return result; 154 } 155 // 156 unsigned long to_ulong() const { 157 imsize_t result = 0; 158 for (imsize_t i = 0, n = (min<imsize_t >)(8, array_size); i < n; ++i) result = x[i] << (8 * i); 159 return result; 160 } 161 // 16 template <class CharT, class Predicate = type_comparison <CharT >, class Allocator = allocator <CharT, array_iterator <CharT >>> 163 string <CharT, Predicate, Allocator > to_string() const { 164 string <CharT, Predicate, Allocator > result; 165 result.reserve(n + 1); 166 // N 167 { 168 unsigned char temp = x[array_size - 1]; 169 // N 8 170 if ((N & 7)!= 0) { 171 for (imsize_t j = 9 - (N & 7); j <= 8; ++j) 17 result.push_back(((temp & (1 << (8 - j))) > 0)? 1 : 0 ); 173 } 174 else { 175 for (imsize_t j = 1; j <= 8; ++j) 176 result.push_back(((temp & (1 << (8 - j))) > 0)? 1 : 0 ); 177 } 178 } 179 for (imsize_t i = ; i <= array_size; ++i) { 180 unsigned char temp = x[array_size - i]; 181 for (imsize_t j = 1; j <= 8; ++j) 18 result.push_back(((temp & (1 << (8 - j))) > 0)? 1 : 0 ); 183 } 184 return result; 185 } 186 //1 187 imsize_t count() const { 188 imsize_t result = 0; 189 for (imsize_t i = 0; i < array_size; ++i) { 190 unsigned char temp = x[i]; 191 for (imsize_t j = 0; j < 8; ++j) 19 result += (temp & (1 << j)) > 0; 193 } 194 return result; 195 } 196 }; 197 }
1 5 n n >> 3 1 n&7 9 10111001 00011010 11010110 n>>3 n&7 10111001 01101000 11010110 n&7 10111001 01101011 11010110 (n>>3+1) n&7 10111001 01101011 01011000 n>>3 01101011 01011000 01011000 n>>3 0 01101011 01011000 00000000 3 template <imsize_t N> 4 class bitset { 5 public: 6 bitset& operator <<=(imsize_t pos) { 7 imsize_t surplus = pos & 7; // 8 imsize_t shift = pos >> 3; // 9 // s u r p l u s 10 for (imsize_t i = 1; i <= array_size; ++i) { 11 x[array_size - i] <<= surplus; 1 // 13 if (array_size!= i) x[array_size - i] = (x[array_size - 1 - i] >> (8 - surplus)); 14 } 15 // s h i f t 16 for (imsize_t i = 1; array_size >= i + shift; ++i) x[array_size - i] = x[array_size - i - shift]; 17 //0 18 for (imsize_t i = 0; i < shift; ++i) x[i] = 0; 19 return byte_check(); 0 } 1 }; } 3 template <imsize_t N> 4 class bitset { 5 public: 6 bitset& operator >>=(imsize_t pos) { 7 imsize_t surplus = pos & 7; // 8 imsize_t shift = pos >> 3; // 9 // s u r p l u s 10 for (imsize_t i = 0; i < array_size; ++i) { 11 x[i] >>= surplus; 1 // 13 if (i + 1!= array_size) x[i] = (x[i + 1] << (8 - surplus)); 14 } 15 // s h i f t 16 for (imsize_t i = 0; i + shift < array_size; ++i) x[i] = x[i + shift]; 17 // 18 for (imsize_t i = 1; i <= shift; ++i) x[array_size - i] = 0; 19 return *this; 0 } 1 }; } 3 // 4 template <imsize_t N> 5 inline bitset <N> operator&(const bitset <N>& b1, const bitset <N>& b) {
1 6 6 bitset <N> temp(b1); 7 return temp &= b; 8 } 9 template <imsize_t N> 10 inline bitset <N> operator (const bitset <N>& b1, const bitset <N>& b) { 11 bitset <N> temp(b1); 1 return temp = b; 13 } 14 template <imsize_t N> 15 inline bitset <N> operatorˆ(const bitset <N>& b1, const bitset <N>& b) { 16 bitset <N> temp(b1); 17 return temp ˆ= b; 18 } 19 template <imsize_t N> 0 inline bitset <N> operator <<(const bitset <N>& b, imsize_t n) { 1 bitset <N> temp(b); return temp <<= n; 3 } 4 template <imsize_t N> 5 inline bitset <N> operator >>(const bitset <N>& b, imsize_t n) { 6 bitset <N> temp(b); 7 return temp >>= n; 8 } 9 }
7.1 1 3 //1 4 struct full_adder { 5 bool c0; // 6 bool s; // 7 8 constexpr full_adder() : c0(false), s(false) {} 9 //c: 10 constexpr full_adder(bool a, bool b, bool c) : c0(a&c b & c a & b), s(aˆbˆc) {} 11 1 void operator()(bool a, bool b, bool c) { c0 = a & c b & c a & b; s = a ˆ b ˆ c; } 13 }; 14 } n 3 // n 4 template <imsize_t N> 5 struct n_full_adder { 6 bool c0; // 7 bitset <N> s; // 8 9 constexpr n_full_adder() : c0(false) {} 10 //c: 11 n_full_adder(const bitset <N>& a, const bitset <N>& b) { 1 bool c = false; // 13 // 14 for (imsize_t i = 0; i < N; ++i) { 15 full_adder temp(a[i], b[i], c); 16 s.set(i, temp.s); 17 c = temp.c0; 18 } 19 c0 = c; 0 } 1 void operator()(const bitset <N>& a, const bitset <N>& b) { 3 bool c = false; // 4 // 5 for (imsize_t i = 0; i < N; ++i) { 6 full_adder temp(a[i], b[i], c); 7 s.set(i, temp.s); 8 c = temp.c0; 9 } 30 c0 = c; 31 } 3 }; 33 }
8 1 int main() { 3 iml::bitset <0> a(13), b(456); 4 // u i n t 5 std::cout << iml::n_full_adder <0>(bit_a, bit_b).s.to_uint() << std::endl; 6 7 return 0; 8 } 1 3 //1 4 struct half_adder { 5 bool c; // 6 bool s; // 7 8 constexpr half_adder() : c(false), s(false) {} 9 constexpr half_adder(bool a, bool b) : c(a&b), s(aˆb) {} 10 11 void operator()(bool a, bool b) { c = a & b; s = a ˆ b; } 1 }; 13 14 // ( 1 ) 15 template <imsize_t N> 16 bitset <N> twos_complement(const bitset <N>& b) { 17 bitset <N> temp1( b); 18 half_adder temp; 19 temp.c = true; 0 for (imsize_t i = 0; (i < N) && temp.c; ++i) { 1 temp(temp1[i], temp.c); temp1.set(i, temp.s); 3 } 4 if (temp.c) temp1.set(n - 1, temp.c); 5 6 return temp1; 7 } 8 } 3 // n 4 template <imsize_t N> 5 struct n_subtractor { 6 bool c0; // ( ) 7 bitset <N> s; // 8 9 constexpr n_subtractor() : c0(false) {} 10 //c: 11 n_subtractor(const bitset <N>& a, const bitset <N>& b) { 1 bool c =true; // 13 // 14 for (imsize_t i = 0; i < N; ++i) { 15 full_adder temp(a[i],!b[i], c); 16 s.set(i, temp.s); 17 c = temp.c0; 18 } 19 c0 = c; 0 } 1 void operator()(const bitset <N>& a, const bitset <N>& b) { 3 bool c = true; // 4 // 5 for (imsize_t i = 0; i < N; ++i) { 6 full_adder temp(a[i],!b[i], c); 7 s.set(i, temp.s); 8 c = temp.c0; 9 } 30 c0 = c; 31 } 3 }; 33 }
9 c0 true 1 int main() { 3 iml::bitset <0> a(13), b(456); 4 // u i n t 5 iml::n_subtractor <0> result(a, b); 6 if (result.c0) std::cout << result.s.to_uint() << std::endl; 7 else std::cout << - << iml:: twos_complement(result.s).to_uint() << std::endl;; 8 9 return 0; 10 }. H SR-FF L Not 3 // HL SR -FF(true:H,false:L) 4 template <bool HL> 5 struct SR_FF { 6 bool q, nq; // 7 8 constexpr SR_FF() : q(false), nq(true) {} 9 SR_FF(bool ini_q, bool s, bool r, bool clock) : q(ini_q), nq(!ini_q) { 10 // n o t 11 clock = (HL)? clock :!clock; 1 bool ns =!(s & clock), nr =!(r & clock); 13 // 14 q =!(ns & nq); 15 nq =!(nr & q); 16 q =!(ns & nq); 17 nq =!(nr & q); 18 } 19 0 void operator()(bool s, bool r, bool clock) { 1 // n o t clock = (HL)? clock :!clock; 3 bool ns =!(s & clock), nr =!(r & clock); 4 // 5 q =!(ns & nq); 6 nq =!(nr & q); 7 q =!(ns & nq); 8 nq =!(nr & q); 9 } 30 }; 31 } D-FF JK-FF T-FF
10 3 // H L D -FF(true:H,false:L) 4 template <bool HL> 5 struct D_FF { 6 bool q, nq; // 7 8 constexpr D_FF() : q(false), nq(true) {} 9 D_FF(bool ini_q, bool d, bool clock) : q(ini_q), nq(!ini_q) { 10 // n o t 11 clock = (HL)? clock :!clock; 1 bool ns =!(d & clock), nr =!(!d & clock); 13 // 14 q =!(ns & nq); 15 nq =!(nr & q); 16 q =!(ns & nq); 17 nq =!(nr & q); 18 } 19 0 void operator()(bool d, bool clock) { 1 // n o t clock = (HL)? clock :!clock; 3 bool ns =!(d & clock), nr =!(!d & clock); 4 // 5 q =!(ns & nq); 6 nq =!(nr & q); 7 q =!(ns & nq); 8 nq =!(nr & q); 9 } 30 31 // clear 3 void clear() { q = false; nq = true; } 33 }; 34 35 // HL JK -FF(true:H,false:L) 36 template <bool HL> 37 struct JK_FF { 38 bool q, nq; // 39 40 constexpr JK_FF() : q(false), nq(true) {} 41 JK_FF(bool ini_q, bool j, bool k, bool clock) : q(ini_q), nq(!ini_q) { 4 // n o t 43 clock = (HL)? clock :!clock; 44 bool ns =!(j & clock & nq), nr =!(k & clock & q); 45 // 46 q =!(ns & nq); 47 nq =!(nr & q); 48 q =!(ns & nq); 49 nq =!(nr & q); 50 } 51 5 void operator()(bool j, bool k, bool clock) { 53 // n o t 54 clock = (HL)? clock :!clock; 55 bool ns =!(j & clock & nq), nr =!(k & clock & q); 56 // 57 q =!(ns & nq); 58 nq =!(nr & q); 59 q =!(ns & nq); 60 nq =!(nr & q); 61 } 6 63 // clear 64 void clear() { q = false; nq = true; } 65 }; 66 67 // H L T -FF(true:H,false:L) 68 template <bool HL> 69 struct T_FF { 70 bool q, nq; // 71 7 constexpr T_FF() : q(false), nq(true) {} 73 T_FF(bool ini_q, bool t, bool clock) : q(ini_q), nq(!ini_q) { 74 // n o t 75 clock = (HL)? clock :!clock; 76 bool ns =!(t & clock), nr =!(t & clock); 77 // 78 q =!(ns & nq); 79 nq =!(nr & q); 80 q =!(ns & nq); 81 nq =!(nr & q); 8 } 83 84 void operator()(bool t, bool clock) { 85 // n o t 86 clock = (HL)? clock :!clock; 87 bool ns =!(t & clock & nq), nr =!(t & clock & q); 88 // 89 q =!(ns & nq); 90 nq =!(nr & q);
11 91 q =!(ns & nq); 9 nq =!(nr & q); 93 } 94 95 // clear 96 void clear() { q = false; nq = true; } 97 }; 98 } 1 D-FF 3 // n 4 template <imsize_t N> 5 struct shift_register { 6 bitset <N> x; // 7 D_FF <true > dff[n]; 8 10 9 shift_register(const bitset <N>& b) : x(b) {} 11 // (b: ) 1 void operator()(bool clock, bool b = false) { 13 dff[0](b, clock); 14 for (imsize_t i = 1; i < N; ++i) { 15 dff[i](x[i - 1], clock); 16 x.set(i - 1, dff[i - 1].q); 17 } 18 x.set(n - 1, dff[n - 1].q); 19 } 0 }; 1 } 1 int main() { 3 iml::shift_register <8> shift(iml::bitset <8>(iml::string <char >("01101101"))); 4 5 for (size_t i = 0; i < 8; ++i) { 6 std::cout << i << : << shift.x.to_string <char >(). c_str() << std::endl; 7 shift(true); 8 } 9 10 return 0; 11 }