-
Notifications
You must be signed in to change notification settings - Fork 63
/
ubitset.cc
34 lines (29 loc) · 1.05 KB
/
ubitset.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// This file is part of the uSTL library, an STL implementation.
//
// Copyright (c) 2005 by Mike Sharov <[email protected]>
// This file is free software, distributed under the MIT License.
#include "ubitset.h"
namespace ustl {
/// Copies bits from \p v of size \p n into \p buf as MSB "1011001..." LSB
/// If \p buf is too small, MSB bits will be truncated.
void convert_to_bitstring (const bitset_value_type* v, size_t n, string& buf) noexcept
{
string::iterator stri = buf.end();
for (size_t i = 0; i < n && stri > buf.begin(); ++ i)
for (bitset_value_type b = 1; b && stri > buf.begin(); b <<= 1)
*--stri = (v[i] & b) ? '1' : '0';
}
/// Copies bits from \p buf as MSB "1011001..." LSB into \p v of size \p n.
void convert_from_bitstring (const string& buf, bitset_value_type* v, size_t n) noexcept
{
string::const_iterator stri = buf.end();
for (size_t i = 0; i < n; ++ i) {
for (bitset_value_type b = 1; b; b <<= 1) {
if (stri == buf.begin() || *--stri == '0')
v[i] &= ~b;
else
v[i] |= b;
}
}
}
} // namespace ustl