This is by far the biggest project i've ever made. The main goal was just to make a bitset class but it expanded into making several classes based off of that main bitset class. Some things I would like suggestions on is how to optimize it better, and if I'm using any bad programming habits or there is a better way of doing something. Thanks.
Classes:
bitset - This is the base program which has no dependencies and is a building block for the other classes
i_bitset - This is used to store integers. It is dependant on the bitset class. It has 5 operator overloads and can convert a bitset to an integer.
c_bitset - This is used to store chars. It is dependant on the i_bitset class. It has 4 operator overloads and can convert a bitset to a char and is used as a building block for s_bitset.
s_bitset - This is used to store strings. It is dependant on the c_bitset class. It has 5 operator overloads and can convert a bitset to a string. This class also contains several standard functions for a string such as append, length, erase and clear.
Here is the github link to the full project: https://github.com/CowNation/cpp-Bitset/
There is too much code to paste the entire project all in this thread, but here is a demo of cpp-Bitset in action, the base bitset class and the i_bitset class:
main.cpp:
#include "s_bitset.h"
int main() {
i_bitset set;
set = 176; // = operator is overloaded
std::cout << set << std::endl; // << operator is overloaded
i_bitset k;
k = 176;
std::cout << (set == 420) << std::endl;
std::cout << (set == k) << std::endl;
c_bitset character;
character = 'k';
std::cout << character << std::endl;
s_bitset ree;
ree.Append(character);
ree.Append("k");
s_bitset xd;
xd = ree + " xd";
std::cout << xd << std::endl;
s_bitset jeff;
jeff = "Jeff"; // = operator is overloaded
std::cout << jeff + " _--" << std::endl; // << and + operator are overloaded
for (int i = 0; i < jeff.length(); i++){
jeff.at(i).print();
std::cout << std::endl;
}
s_bitset st;
std::cout << "Enter string: ";
std::cin >> st;
std::cout << st << std::endl;
}
bitset.h:
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
class bitset {
protected:
std::vector< bool >BitSet;
int GetValue(int Index) const;
public:
bitset(int bits);
void setbit(int Index, bool Value);
bool getbit(int Index);
void print();
};
bitset.cpp:
#include "bitset.h"
int bitset::GetValue(int Index) const {
return pow(2, Index);
}
bitset::bitset(int bits){
BitSet.clear();
for(int i = 0; i < bits; i++){
BitSet.push_back(false);
}
}
void bitset::setbit(int Index, bool Value){
BitSet[Index] = Value;
}
bool bitset::getbit(int Index){
return BitSet[Index];
}
void bitset::print(){
for (int i = 0; i < BitSet.size(); i++)
std::cout << BitSet[i] << "/" << GetValue(i) << " ";
}
i_bitset.h:
#include "bitset.h"
class i_bitset : public bitset {
private:
int pGet() const;
public:
i_bitset() : bitset(8){}
friend std::ostream & operator<<(std::ostream & _stream, i_bitset const & mc);
operator int() const;
int operator=(const int& b);
bool operator==(const i_bitset& b);
bool operator==(const int& b);
};
i_bitset Integer(int i);
i_bitset.cpp:
#include "i_bitset.h"
int i_bitset::pGet() const {
int ret = 0;
for (int i = 0; i < BitSet.size(); i++)
ret += BitSet[i] * GetValue(i);
return ret;
}
std::ostream & operator<<(std::ostream & _stream, i_bitset const & mc) {
_stream << mc.pGet();
return _stream;
}
i_bitset::operator int() const {
return pGet();
}
void checkValue(int& iLetter, i_bitset& integer, int Num, int Index){
if (iLetter >= Num && integer.getbit(Index) == false){
integer.setbit(Index, true);
iLetter -= Num;
}
}
i_bitset Integer(int i){
i_bitset integer;
while (i > 0){
checkValue(i, integer, 128, 7);
checkValue(i, integer, 64, 6);
checkValue(i, integer, 32, 5);
checkValue(i, integer, 16, 4);
checkValue(i, integer, 8, 3);
checkValue(i, integer, 4, 2);
checkValue(i, integer, 2, 1);
checkValue(i, integer, 1, 0);
}
return integer;
}
int i_bitset::operator=(const int& b) {
i_bitset integer = Integer(b);
for (int i = 0; i < 8; i++){
setbit(i, integer.getbit(i));
}
return b;
}
bool i_bitset::operator==(const i_bitset& b){
return (int)b == pGet();
}
bool i_bitset::operator==(const int& b){
return b == pGet();
}