// file main_04_UDEF_e.cpp #include "../../helpers/println.hpp" #include const int bitsPerOctet = 8; struct BinaryOctet { public: bool evenParity; // set to true if number of '1' in bitsAsDigits is even, false otherwise char bitsAsDigits[bitsPerOctet]{}; // bit values as chars BinaryOctet(int x = 0); BinaryOctet& operator=(int const &a); BinaryOctet(const BinaryOctet &) = default; private: BinaryOctet init(int x); }; BinaryOctet BinaryOctet::init(int x) { int length = static_cast(std::to_string(x).length() * 4); while (x >= 1) { bitsAsDigits[length] = (x % 2 == 0) ? '1' : '0'; x = x / 2; length--; } int i = 0; for (char c : bitsAsDigits) { if (c == '1') i++; } evenParity = i % 2 == 0; return *this; } BinaryOctet::BinaryOctet(int x) { init(x); } bool operator!=(BinaryOctet a, BinaryOctet b) { if(a.evenParity != b.evenParity) return false; for (int i = 0; i < bitsPerOctet; ++i) { if(a.bitsAsDigits[i] != b.bitsAsDigits[i]) return false; } return true; } BinaryOctet operator--(BinaryOctet &id, int) { return id; } BinaryOctet operator+(BinaryOctet a, BinaryOctet b) { BinaryOctet result; for (int i = 0; i < bitsPerOctet; ++i) { if(a.bitsAsDigits[i] == b.bitsAsDigits[i]) result.bitsAsDigits[i] = '0'; else result.bitsAsDigits[i] = '1'; } return result; } BinaryOctet operator/(BinaryOctet a, BinaryOctet b) { } BinaryOctet& BinaryOctet::operator=(int const &i) { init(i); return *this; } BinaryOctet doCalculation(BinaryOctet a, BinaryOctet b) { BinaryOctet result; for (; a != b; b--) { a = a + 1; a = a / b; } result = a + b; return result; } // for println(); std::string as_string(BinaryOctet a) { std::string result = "("; for (char c : a.bitsAsDigits) { result += c; } result += ")"; return result; } // for std::cout std::ostream& operator<< (std::ostream& os, const BinaryOctet &toBePrinted){ os << as_string(toBePrinted); return os; } int main(int argc, char **argv) { BinaryOctet a = 0b00001111; BinaryOctet b = 0b00000110; std::cout << new BinaryOctet(5) << std::endl; println(a + b); std::cout << a + b << std::endl; //println("result = ", doCalculation(a,b)); //std::cout << "result = " << doCalculation(a,b) << std::endl; return 0; }