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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
//part 1
#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)));
}
}
|