Merge remote-tracking branch 'origin/master'

# Conflicts:
#	src/04_UDEF/MP/main_04_UDEF_e.cpp
This commit is contained in:
Johannes Theiner 2018-10-07 14:07:13 +02:00
commit b15927019a
1 changed files with 66 additions and 47 deletions

View File

@ -6,13 +6,14 @@ const int bitsPerOctet = 8;
struct BinaryOctet { struct BinaryOctet {
bool evenParity; bool evenParity;
char bitsAsDigits[bitsPerOctet]{}; char bitsAsDigits[bitsPerOctet]{};
BinaryOctet(int x = 0); BinaryOctet(int x = 0);
BinaryOctet(const BinaryOctet&) = default; BinaryOctet(const BinaryOctet&) = default;
BinaryOctet &operator=(int &a); const BinaryOctet operator--(int);
const BinaryOctet operator++(int);
bool operator!=(BinaryOctet &a); bool operator!=(BinaryOctet &a);
bool operator==(BinaryOctet &a); bool operator>=(BinaryOctet &a);
BinaryOctet operator--(int); BinaryOctet operator-(BinaryOctet a);
BinaryOctet operator-(BinaryOctet &a);
BinaryOctet operator+(BinaryOctet a); BinaryOctet operator+(BinaryOctet a);
BinaryOctet operator/(BinaryOctet &a); BinaryOctet operator/(BinaryOctet &a);
}; };
@ -21,54 +22,32 @@ BinaryOctet::BinaryOctet(int x) {
for (char &bitsAsDigit : bitsAsDigits) { for (char &bitsAsDigit : bitsAsDigits) {
bitsAsDigit = '0'; bitsAsDigit = '0';
} }
for (int i = 0; i <= bitsPerOctet - 1; i++) {
int tmp = ((x >> i) & 1); int remainder;
bitsAsDigits[i] = (tmp == 1) ? '1' : '0'; int temp = 0;
while (x != 0) {
remainder = x % 2;
x /= 2;
bitsAsDigits[temp] = static_cast<char>(remainder + '0');
temp++;
} }
//FIXME: das sollte eigentlich nicht benötigt werden
std::reverse(std::begin(bitsAsDigits), std::end(bitsAsDigits)); std::reverse(std::begin(bitsAsDigits), std::end(bitsAsDigits));
//einsen zählen int ones = 0;
int i = 0;
for (char c : bitsAsDigits) { for (char c : bitsAsDigits) {
if (c == '1') i++; if (c == '1') ones++;
} }
evenParity = i % 2 == 0; evenParity = ones % 2 == 0;
//std::cout << evenParity << std::endl;
} }
BinaryOctet &BinaryOctet::operator=(int &a) { const BinaryOctet BinaryOctet::operator--(int) {
std::string tmp = std::to_string(a);
for (int i = 0; i >= tmp.length(); i++) {
bitsAsDigits[i] = tmp[i];
}
return *this;
}
BinaryOctet BinaryOctet::operator--(int) {
return operator-(*new BinaryOctet(1)); return operator-(*new BinaryOctet(1));
} }
BinaryOctet BinaryOctet::operator-(BinaryOctet &a) { const BinaryOctet BinaryOctet::operator++(int) {
BinaryOctet result; return operator+(*new BinaryOctet(1));
bool over = false;
for(int i = bitsPerOctet - 1; i >= 0; --i) {
int tmp;
int aI = a.bitsAsDigits[i] - '0';
int bI = bitsAsDigits[i] - '0';
if(over) tmp = bI - aI - 1;
else tmp = bI - aI;
over = false;
if(tmp < 0) {
over = true;
tmp = tmp + 1;
}
result.bitsAsDigits[i] = static_cast<char>(tmp + '0');
}
return result;
} }
BinaryOctet BinaryOctet::operator+(BinaryOctet a) { BinaryOctet BinaryOctet::operator+(BinaryOctet a) {
@ -98,26 +77,63 @@ BinaryOctet BinaryOctet::operator+(BinaryOctet a) {
return result; return result;
} }
BinaryOctet BinaryOctet::operator/(BinaryOctet &a) { BinaryOctet BinaryOctet::operator-(BinaryOctet a) {
BinaryOctet result; BinaryOctet result;
bool over = false;
for(int i = bitsPerOctet - 1; i >= 0; --i) {
int tmp;
int aI = a.bitsAsDigits[i] - '0';
int bI = bitsAsDigits[i] - '0';
if(over) tmp = bI - aI - 1;
else tmp = bI - aI;
over = false;
if(tmp < 0) {
over = true;
tmp = tmp + 1;
}
result.bitsAsDigits[i] = static_cast<char>(tmp + '0');
}
return result; return result;
} }
bool BinaryOctet::operator==(BinaryOctet &a) { BinaryOctet BinaryOctet::operator/(BinaryOctet &a) {
return !operator!=(a); BinaryOctet result = *new BinaryOctet(0);
while(*this >= a) {
*this = *this - a;
result = result + 1;
std::cout << result.bitsAsDigits << std::endl;
}
return result;
} }
bool BinaryOctet::operator!=(BinaryOctet &a) { bool BinaryOctet::operator!=(BinaryOctet &a) {
if (a.evenParity != evenParity) return false; if (a.evenParity !=evenParity) return false;
for (int i = 0; i < bitsPerOctet; ++i) { for (int i = 0; i < bitsPerOctet; ++i) {
if (a.bitsAsDigits[i] != bitsAsDigits[i]) return false; if (a.bitsAsDigits[i] != bitsAsDigits[i]) return false;
} }
return true; return true;
} }
//TODO: this
bool BinaryOctet::operator>=(BinaryOctet &a) {
if(!operator!=(a)) return true;
int aI = 0;
for(int i = bitsPerOctet - 1; i >= 0; --i) {
aI =+ (a.bitsAsDigits[i] - '0') * 2 ^ i;
}
int bI = 0;
for(int i = bitsPerOctet - 1; i >= 0; --i) {
bI += (bitsAsDigits[i] - '0') * 2 ^ i;
}
return bI >= aI;
}
BinaryOctet doCalculation(BinaryOctet a, BinaryOctet b) { BinaryOctet doCalculation(BinaryOctet a, BinaryOctet b) {
BinaryOctet result; BinaryOctet result;
for (; a != b; b--) { for (; a != b; b--) {
@ -178,11 +194,14 @@ int main(int argc, char **argv) {
std::cout << "========= operator- ==========" << std::endl; std::cout << "========= operator- ==========" << std::endl;
std::cout << "a - b = " << a - b << std::endl; std::cout << "a - b = " << a - b << std::endl;
std::cout << "========= operator/ ==========" << std::endl;
std::cout << "a / b = " << a / b << std::endl;
std::cout << "======== operator-- ============" << std::endl; std::cout << "======== operator-- ============" << std::endl;
BinaryOctet d = 0b111111111; BinaryOctet d = 0b111111111;
d--; d--;
std::cout << d-- << std::endl; std::cout << "11111111-- = " << d-- << std::endl;
std::cout << "============== doCalculation ================" << std::endl; std::cout << "============== doCalculation ================" << std::endl;
std::cout << "result = " << doCalculation(a, b) << std::endl; std::cout << "result = " << doCalculation(a, b) << std::endl;