196 lines
4.6 KiB
C++
196 lines
4.6 KiB
C++
// file main_04_UDEF_e.cpp
|
|
#include <iostream>
|
|
#include <algorithm>
|
|
|
|
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();
|
|
BinaryOctet(int x);
|
|
BinaryOctet(const BinaryOctet &) = default;
|
|
BinaryOctet &operator=(int const &a);
|
|
const BinaryOctet operator--(int);
|
|
const BinaryOctet operator-(BinaryOctet a);
|
|
const BinaryOctet operator+(BinaryOctet a);
|
|
};
|
|
|
|
BinaryOctet::BinaryOctet() {
|
|
for (char &bitsAsDigit : bitsAsDigits) {
|
|
bitsAsDigit = '0';
|
|
}
|
|
evenParity = true;
|
|
}
|
|
|
|
|
|
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';
|
|
}
|
|
//FIXME: das sollte eigentlich nicht benötigt werden
|
|
std::reverse(std::begin(bitsAsDigits), std::end(bitsAsDigits));
|
|
|
|
//einsen zählen
|
|
int i = 0;
|
|
for (char c : bitsAsDigits) {
|
|
if (c == '1') i++;
|
|
}
|
|
|
|
evenParity = i % 2 == 0;
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
//TODO: this method
|
|
const BinaryOctet BinaryOctet::operator--(const int) {
|
|
return operator-(1);
|
|
}
|
|
|
|
//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;
|
|
|
|
result = *this + a;
|
|
std::cout << result.evenParity << std::endl;
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
const BinaryOctet BinaryOctet::operator+(BinaryOctet a) {
|
|
BinaryOctet result;
|
|
bool over = false;
|
|
for (int i = bitsPerOctet - 1; i >= 0; --i) {
|
|
//Arbeitsvariablen anlegen und char in int umwandeln für einfachere Rechnung
|
|
int tmp;
|
|
int aI = a.bitsAsDigits[i] - '0';
|
|
int bI = bitsAsDigits[i] - '0';
|
|
|
|
|
|
if(over) tmp = aI + bI + 1;
|
|
else tmp = aI + bI;
|
|
over = false;
|
|
|
|
|
|
if(tmp >= 2) {
|
|
over = true;
|
|
if (tmp % 2 == 0) tmp = 0;
|
|
else tmp = 1;
|
|
}
|
|
|
|
//Umwandlung in char und ins Ergebnis schreiben
|
|
result.bitsAsDigits[i] = static_cast<char>(tmp + '0');
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
BinaryOctet operator/(BinaryOctet a, BinaryOctet b) {
|
|
BinaryOctet result;
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
BinaryOctet &BinaryOctet::operator=(int const &a) {
|
|
std::string tmp = std::to_string(a);
|
|
for (int i = 0; i >= tmp.length(); i++) {
|
|
bitsAsDigits[i] = tmp[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;
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
std::ostream &operator<<(std::ostream &os, BinaryOctet *toBePrinted) {
|
|
os << as_string(toBePrinted);
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
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 = " << c << 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;
|
|
return 0;
|
|
}
|