04_UDEF_MP BinaryOctet weitere fehlerhafte Operatoren

This commit is contained in:
Johannes Theiner 2018-09-28 10:24:55 +02:00
parent 9aa4d96928
commit 3f7f982922
1 changed files with 39 additions and 15 deletions

View File

@ -13,6 +13,7 @@ public:
BinaryOctet(const BinaryOctet &) = default; BinaryOctet(const BinaryOctet &) = default;
BinaryOctet &operator=(int const &a); BinaryOctet &operator=(int const &a);
const BinaryOctet operator--(int); const BinaryOctet operator--(int);
const BinaryOctet operator-(BinaryOctet a);
const BinaryOctet operator+(BinaryOctet a); const BinaryOctet operator+(BinaryOctet a);
}; };
@ -25,6 +26,9 @@ BinaryOctet::BinaryOctet() {
BinaryOctet::BinaryOctet(int x) { BinaryOctet::BinaryOctet(int x) {
for (char &bitsAsDigit : bitsAsDigits) {
bitsAsDigit = '0';
}
for (int i = 0; i <= bitsPerOctet - 1; i++) { for (int i = 0; i <= bitsPerOctet - 1; i++) {
int tmp = ((x >> i) & 1); int tmp = ((x >> i) & 1);
bitsAsDigits[i] = (tmp == 1) ? '1' : '0'; bitsAsDigits[i] = (tmp == 1) ? '1' : '0';
@ -53,16 +57,23 @@ bool operator!=(BinaryOctet a, BinaryOctet b) {
//TODO: this method //TODO: this method
const BinaryOctet BinaryOctet::operator--(const int) { const BinaryOctet BinaryOctet::operator--(const int) {
bool over = false; return operator-(1);
for(int i = bitsPerOctet - 1; i >= 0; --i) { }
int tmp = bitsAsDigits[i] - '0';
//FIXME: returns wrong result
//using complement method here
bitsAsDigits[i] = static_cast<char>(tmp + '0'); const BinaryOctet BinaryOctet::operator-(BinaryOctet a) {
BinaryOctet result;
//reverse a
for(int i = 0; i <= bitsPerOctet; i++) {
a.bitsAsDigits[i] = a.bitsAsDigits[i] == '0' ? '1' : '0';
} }
a + 1;
return *this; result = *this + a;
std::cout << result.evenParity << std::endl;
return result;
} }
@ -157,15 +168,28 @@ std::ostream &operator<<(std::ostream &os, BinaryOctet *toBePrinted) {
int main(int argc, char **argv) { int main(int argc, char **argv) {
BinaryOctet a = 0b00001111;
std::cout << a << std::endl; std::cout << "========= init ==========" << std::endl;
BinaryOctet b = 0b00000110; BinaryOctet a = 0b00001111; //15
std::cout << b << std::endl; std::cout << "a = " << a << std::endl;
b--; BinaryOctet b = 0b00000110;//6
std::cout << b << std::endl; std::cout << "b = " << b << std::endl;
auto *c = new BinaryOctet(5); auto *c = new BinaryOctet(5);
std::cout << c << std::endl; std::cout << "c = " << c << std::endl;
std::cout << a + b << std::endl;
std::cout << "======== operator+ ===========" <<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;
BinaryOctet d = 0b111111111;
d--;
std::cout << d-- << std::endl;
std::cout << "============== doCalculation ================" << std::endl;
std::cout << "result = " << doCalculation(a, b) << std::endl; std::cout << "result = " << doCalculation(a, b) << std::endl;
return 0; return 0;
} }