Added convertation to string to BitArray
This commit is contained in:
parent
105887d468
commit
f932df3253
@ -1,11 +1,24 @@
|
||||
#include "pch.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "BitArray.hpp"
|
||||
|
||||
bool BitArray::get(unsigned index)
|
||||
bool BitArray::get(unsigned index) const
|
||||
{
|
||||
return (v[index / 8] >> (8 - 1 - index % 8)) & 1;
|
||||
}
|
||||
|
||||
BitArray::operator std::string() const
|
||||
{
|
||||
std::stringstream res;
|
||||
|
||||
for (unsigned i = 0; i < size; i++)
|
||||
res << static_cast<int>((*this).get(i));
|
||||
|
||||
return res.str();
|
||||
}
|
||||
|
||||
void BitArray::set(unsigned index, bool val)
|
||||
{
|
||||
if (val)
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@ -13,10 +14,12 @@ class BitArray
|
||||
public:
|
||||
BitArray(unsigned size_ = 0) : size{ size_ }, v(ceil_div(size_, 8)) {};
|
||||
|
||||
operator std::string() const;
|
||||
|
||||
unsigned size;
|
||||
vector<unsigned char> v;
|
||||
|
||||
bool get(unsigned index);
|
||||
bool get(unsigned index) const;
|
||||
void set(unsigned index, bool val);
|
||||
unsigned set(unsigned index, int32_t val, unsigned size);
|
||||
};
|
||||
|
@ -24,6 +24,10 @@ TEST_F(BitArrayTests, GetsBitByIndex) {
|
||||
EXPECT_EQ(a1.get(1), 0);
|
||||
}
|
||||
|
||||
TEST_F(BitArrayTests, Converts_to_string) {
|
||||
EXPECT_EQ(std::string(a1), "101010101111111");
|
||||
}
|
||||
|
||||
TEST_F(BitArrayTests, SetsIndividualBit) {
|
||||
a1.set(0, 0);
|
||||
a1.set(1, 1);
|
||||
|
Loading…
x
Reference in New Issue
Block a user