Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include<string>
- #include<iostream>
- using namespace std;
- struct b {
- private:
- byte control;
- size_t size;
- size_t index;
- unsigned long long realsize;
- int bit;
- byte* _byte;
- public:
- b() : index(0), bit(0), size(1), realsize(1), control(0) {
- _byte = new byte[1]{};
- this->populate();
- }
- b(unsigned long long i) : index(0), bit(0), size(ceil(i/8.0) ? ceil(i / 8.0) : 1), realsize(i),control(0) {
- _byte = new byte[ size ];
- this->populate();
- }
- b(unsigned long long i, byte* bi) : index(0), bit((i % 8)), size(ceil(i / 8.0)), realsize(i),control(*bi) {
- _byte = new byte[size];
- this->populate();
- this->clearRemainder();
- }
- b(unsigned long long i, bool bo) : index(0), bit((i % 8)), size(i / 8.0 ? ceil(i / 8.0) : 1), realsize(i), control( bo==1 ? 255 : 0) {
- _byte = new byte[size];
- this->populate();
- this->clearRemainder();
- }
- ~b() {
- delete[] _byte;
- }
- private:
- void realocate() {
- byte* t = (byte*)malloc(this->size+1); //get new memory
- if (t) {
- ZeroMemory(t, size + 1);
- memcpy(t, this->_byte, size);
- delete[] this->_byte;
- this->_byte = t; //watch this go down in flames.
- size++;
- }
- }
- void realocate(unsigned long long ull) {
- this->setIndexAndBit(ull);
- byte* t = (byte*)malloc(this->index + 1);
- if (t) {
- ZeroMemory(t, index + 1);
- memcpy(t, this->_byte, size);
- delete[] this->_byte;
- this->_byte = t;
- size = index + 1;
- }
- }
- void setIndexAndBit(unsigned long long i) {
- this->bit = i % 8;
- this->index = (i - bit) > 7 ? (i - bit) >> 3 : 0;
- }
- void populate() {
- for (size_t i = 0; i < size;i++) {
- _byte[i] = control;
- }
- }
- void clearRemainder() {
- if (!bit) { return; }
- for (int i=7;i> bit-1; i--) {
- this->_byte[size - 1] &= ~(1 << (7 - i));
- }
- bit = 0;
- }
- string ByteToString() {
- string s{};
- for (int i = 0; i < 8; i++) {
- if (checkbit(i)) { s.push_back('1'); }
- else { s.push_back('0'); }
- }
- return s;
- }
- bool checkbit(int bi) {
- if (this->index + 1 > this->size) { return false; }
- if (this->_byte[this->index] == (this->_byte[index] | (1 << (7 - bi)))) {
- return true;
- }
- return false;
- }
- bool checkbit(byte bi, int i) {
- if (bi == (bi | (1 << (7 - i)))) {
- return true;
- }
- return false;
- }
- void bitshiftright(unsigned long long ull) {
- unsigned long long s = (unsigned long long)this->size;
- if (realsize + ull > s * 8) {
- this->realocate(realsize + ull);
- }
- for (unsigned long long i = realsize - 1; ; i--) {
- this->SetBit(i+ull, test(i));
- if (i == 0) { break; }
- }
- for (unsigned long long i = 0; i < ull; i++) { //cleans up the left
- this->SetBit(i, 0);
- this->SetBit(realsize + i, 0);
- }
- this->shrink();
- }
- void bitshiftleft(unsigned long long ull) {
- unsigned long long s = (unsigned long long)this->size;
- if (realsize + ull > s * 8) {
- this->realocate(realsize + ull);
- }
- for (unsigned long long i = 0; i < realsize; i++) {
- this->SetBit(i, test(i + ull));
- }
- for (unsigned long long i = realsize; i < ull + realsize; i++) { //cleans up the right
- this->SetBit(i, 0);
- }
- }
- void alignright() {
- while (!this->_byte[this->size - 1]) {
- for (auto i = size - 1; i > 0; i--) {
- if (!this->_byte[i]) {
- this->_byte[i] = this->_byte[i - 1];
- this->_byte[i - 1] = 0;
- }
- }
- }
- unsigned long long s = (unsigned long long)this->size;
- this->realsize = s * 8;
- }
- bool compare(bool bo) {
- if (bo) {
- return _byte[index] == (_byte[index] | (1 << (7 - bit)));
- }
- else {
- return _byte[index] == (_byte[index] & ~(1 << (7 - bit)));
- }
- }
- b& copy(b& other) {
- while (other.realsize > this->realsize) { this->pushback(false); }
- memcpy(this->_byte, other._byte, size);
- return *this;
- }
- public:
- void shrink() {
- unsigned long long ull = size - 1;
- while (_byte[ull] == 0) {
- ull--;
- if (ull == 0) { break; }
- }
- this->resize(((ull+1) << 3));
- }
- void operator>>(unsigned long long ull) {
- this->bitshiftright(ull);
- }
- void operator<<(unsigned long long ull) {
- return this->bitshiftleft(ull);
- }
- bool operator==(bool bo) {
- return this->compare(bo);
- }
- void operator=(bool bo) {
- if (bo) {
- this->_byte[index] |= (1 << (7 - bit));
- }
- else {
- this->_byte[index] &= (~(1 << (7 - bit)));
- }
- }
- b& operator=(b& other) {
- return this->copy(other);
- }
- b& operator-(b& other) {
- }
- b& subtract(b& other) { //untested not finished
- if (this->realsize < other.realsize) {
- while (this->realsize < other.realsize) { this->bitshiftright(1); } //not efficient
- }
- else if (other.realsize < this->realsize) {
- while (other.realsize < this->realsize) { other.bitshiftright(1); }
- }
- //this->bitshiftright(1);
- //other.bitshiftright(1);
- other.shrink();
- this->shrink();
- for (unsigned long long i = 0; i < realsize; i++) {
- if ( this->test(i) == 1 && other.test(i) == 0 ) {
- this->SetBit(i, 1);//unecessary
- }
- else if (this->test(i) == 1 && other.test(i) == 1) {
- this->SetBit(i, 0);
- }
- else if (this->test(i) == 0 && other.test(i) == 1) {
- for (unsigned long long j = i;; j--) {
- if (this->test(j) == 0) {
- this->SetBit(j, 1);
- }
- else if (this->test(j) == 1) {
- this->SetBit(j, 0);
- break;
- }
- if (j == 0) { break; } //gets to here it would be negitive... not sure how to handle that
- }
- }
- }
- //this->bitshiftright(7);
- //this->shrink();
- return *this;
- }
- b& operator[](unsigned long long i) {
- this->setIndexAndBit(i) ;
- if (index + 1 > size) {
- this->realocate();
- this->realsize = i + 1;
- }
- return *this;
- }
- b& operator+(b& other) {
- return this->add(other);
- }
- b& add(b& other) {
- while (this->realsize < other.realsize) { this->pushback(false); }
- while (other.realsize < this->realsize) { other.pushback(false); }
- /*this->print();
- cout << "\n";
- other.print();*/
- this->pushback(false);
- other.pushback(false);
- this->bitshiftright(1);
- other.bitshiftright(1);
- this->alignright();
- other.alignright();
- //this->print();
- //cout << "\n";
- //other.print();
- for (unsigned long long i = 0; i < realsize; i++) {
- if ((this->test(i) == 0 && other.test(i) == 1) || (this->test(i) == 1 && other.test(i) == 0)) {
- this->SetBit(i, 1);
- }
- else if (this->test(i) == 1 && other.test(i) == 1) {
- for (unsigned long long j = i;; j--) {
- if (this->test(j) == 1) {
- this->SetBit(j, 0);
- }
- else if(this->test(j)==0){
- this->SetBit(j, 1);
- break;
- }
- if (j == 0) { break; } //if it gets to here something went very wrong
- }
- }
- }
- return *this;
- }
- void resize(unsigned long long ull) {
- this->setIndexAndBit(ull-1);
- byte* t = (byte*)malloc(this->index + 1);
- if (t) {
- ZeroMemory(t, index + 1);
- memcpy(t, this->_byte, index + 1);
- delete[] this->_byte;
- this->_byte = t;
- size = index + 1;
- }
- this->realsize = ull; //this could be clear remainder...
- int i = 0;
- while ((realsize + i) % 8 != 0) {
- this->SetBit(realsize + i, 0);
- i++;
- }
- }
- unsigned long long capacity() {
- unsigned long long s = (unsigned long long)this->size;
- return s << 3;
- }
- void SetBit(unsigned long long ull, bool bo) {
- unsigned long long s = (unsigned long long)this->size;
- if (ull > (s<<3) - 1) {
- this->realocate(ull);
- this->realsize = ull + 1;
- }
- this->setIndexAndBit(ull);
- if (bo) {
- this->_byte[index] |= (1 << (7 - bit));
- }
- else {
- this->_byte[index] &= (~(1 << (7 - bit)));
- }
- }
- bool isEmpty() {
- return !realsize;
- }
- void swap(unsigned long long b1,unsigned long long b2) {
- bool bb1 = this->test(b1);
- this->SetBit(b1,this->test(b2));
- this->SetBit(b2, bb1);
- }
- bool test(unsigned long long ull) {
- this->setIndexAndBit(ull);
- return this->checkbit(bit);
- }
- void pushback(bool bo) {
- unsigned long long s = (unsigned long long)this->size;
- if (realsize+1 > s << 3 ) {
- this->realocate();
- }
- realsize++;
- this->setIndexAndBit(realsize - 1);
- this->SetBit(realsize - 1, bo);
- }
- void pushback(byte* bp) { //this might be useless
- for (int i = 0; i < 8; i++) {
- this->pushback(checkbit(*bp, i));
- }
- }
- void clear() {
- this->control = 0b00000000;
- this->populate();
- }
- void popback() {
- this->SetBit(realsize - 1, 0);
- realsize--;
- }
- void insert(unsigned long long ull, bool bo) {
- unsigned long long s = (unsigned long long)this->size;
- if (ull > ( s << 3 ) -1 ) {
- this->SetBit(ull,bo);
- return;
- }
- else if (realsize + 1 > s << 3 ) {
- this->realocate();
- }
- for (unsigned long long i = realsize; i > ull; i--) {
- this->swap(i, i - 1);
- }
- this->SetBit(ull, bo);
- realsize++;
- }
- void flip(unsigned long long ull) {
- this->setIndexAndBit(ull);
- if (this->checkbit(bit)) {
- this->SetBit(ull, 0);
- }
- else { this->SetBit(ull, 1); }
- }
- void print() {
- for (size_t i = 0; i < size; i++) {
- index = i;
- cout << this->ByteToString() << "\n";
- }
- }
- unsigned long long count() {
- unsigned long long c{0};
- for (unsigned long long i = 0; i < realsize; i++) {
- if (this->test(i)) {
- c++;
- }
- }
- return c;
- }
- unsigned long long Size() {
- return this->realsize;
- }
- };
- int main() {
- b a(16, true);
- b ab(8, true);
- b c{};
- ab.SetBit(21, true);
- //cout << ab.Size();
- //cout << "\n";
- //ab.print();
- //cout << "\n";
- //a.print();
- c = a + ab;
- c.print();
- //cout << "\n";
- //c >> 5;
- //c.print();
- }
Advertisement
Add Comment
Please, Sign In to add comment