Build system improvements
[ustl.git] / ubitset.cc
blob21b5a7a8c636539d65b07c64412aa3af6f1cb926
1 // This file is part of the ustl library, an STL implementation.
2 //
3 // Copyright (C) 2005 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
5 //
6 // ubitset.cc
7 //
9 #include "ubitset.h"
11 namespace ustl {
13 /// Copies bits from \p v of size \p n into \p buf as MSB "1011001..." LSB
14 /// If \p buf is too small, MSB bits will be truncated.
15 void convert_to_bitstring (const bitset_value_type* v, size_t n, string& buf)
17 string::iterator stri = buf.end();
18 for (size_t i = 0; i < n && stri > buf.begin(); ++ i)
19 for (bitset_value_type b = 1; b && stri > buf.begin(); b <<= 1)
20 *--stri = (v[i] & b) ? '1' : '0';
23 /// Copies bits from \p buf as MSB "1011001..." LSB into \p v of size \p n.
24 void convert_from_bitstring (const string& buf, bitset_value_type* v, size_t n)
26 string::const_iterator stri = buf.end();
27 for (size_t i = 0; i < n; ++ i) {
28 for (bitset_value_type b = 1; b; b <<= 1) {
29 if (stri == buf.begin() || *--stri == '0')
30 v[i] &= ~b;
31 else
32 v[i] |= b;
37 } // namespace ustl