From 3f7f9829223980590f3cb85274325315cfc99edf Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Fri, 28 Sep 2018 10:24:55 +0200 Subject: [PATCH] 04_UDEF_MP BinaryOctet weitere fehlerhafte Operatoren --- 04_UDEF/MP/main_04_UDEF_e.cpp | 54 +++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/04_UDEF/MP/main_04_UDEF_e.cpp b/04_UDEF/MP/main_04_UDEF_e.cpp index d29b19c..cc84326 100644 --- a/04_UDEF/MP/main_04_UDEF_e.cpp +++ b/04_UDEF/MP/main_04_UDEF_e.cpp @@ -13,6 +13,7 @@ public: BinaryOctet(const BinaryOctet &) = default; BinaryOctet &operator=(int const &a); const BinaryOctet operator--(int); + const BinaryOctet operator-(BinaryOctet a); const BinaryOctet operator+(BinaryOctet a); }; @@ -25,6 +26,9 @@ BinaryOctet::BinaryOctet() { BinaryOctet::BinaryOctet(int x) { + for (char &bitsAsDigit : bitsAsDigits) { + bitsAsDigit = '0'; + } for (int i = 0; i <= bitsPerOctet - 1; i++) { int tmp = ((x >> i) & 1); bitsAsDigits[i] = (tmp == 1) ? '1' : '0'; @@ -53,16 +57,23 @@ bool operator!=(BinaryOctet a, BinaryOctet b) { //TODO: this method const BinaryOctet BinaryOctet::operator--(const int) { - bool over = false; - for(int i = bitsPerOctet - 1; i >= 0; --i) { - int tmp = bitsAsDigits[i] - '0'; + return operator-(1); +} - - - bitsAsDigits[i] = static_cast(tmp + '0'); +//FIXME: returns wrong result +//using complement method here +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) { - BinaryOctet a = 0b00001111; - std::cout << a << std::endl; - BinaryOctet b = 0b00000110; - std::cout << b << std::endl; - b--; - std::cout << b << std::endl; + + std::cout << "========= init ==========" << std::endl; + BinaryOctet a = 0b00001111; //15 + std::cout << "a = " << a << std::endl; + BinaryOctet b = 0b00000110;//6 + std::cout << "b = " << b << std::endl; auto *c = new BinaryOctet(5); - std::cout << c << std::endl; - std::cout << a + b << std::endl; + std::cout << "c = " << c << std::endl; + + std::cout << "======== operator+ ===========" <