2018-03-09 09:47:53 +01:00
|
|
|
// file main_04_UDEF_e.cpp
|
|
|
|
#include <iostream>
|
2018-09-05 01:03:06 +02:00
|
|
|
#include <algorithm>
|
2018-03-09 09:47:53 +01:00
|
|
|
|
2018-06-02 10:50:37 +02:00
|
|
|
const int bitsPerOctet = 8;
|
2018-03-09 09:47:53 +01:00
|
|
|
|
|
|
|
struct BinaryOctet {
|
2018-08-29 15:44:59 +02:00
|
|
|
public:
|
2018-06-02 10:50:37 +02:00
|
|
|
bool evenParity; // set to true if number of '1' in bitsAsDigits is even, false otherwise
|
2018-08-29 15:44:59 +02:00
|
|
|
char bitsAsDigits[bitsPerOctet]{}; // bit values as chars
|
2018-09-05 01:03:06 +02:00
|
|
|
BinaryOctet();
|
|
|
|
BinaryOctet(int x);
|
2018-06-02 10:50:37 +02:00
|
|
|
BinaryOctet(const BinaryOctet &) = default;
|
2018-09-05 01:03:06 +02:00
|
|
|
BinaryOctet &operator=(int const &a);
|
2018-08-31 18:26:38 +02:00
|
|
|
const BinaryOctet operator--(int);
|
2018-03-09 09:47:53 +01:00
|
|
|
};
|
|
|
|
|
2018-09-05 01:03:06 +02:00
|
|
|
BinaryOctet::BinaryOctet() {
|
2018-08-31 18:26:38 +02:00
|
|
|
for (char &bitsAsDigit : bitsAsDigits) {
|
|
|
|
bitsAsDigit = '0';
|
|
|
|
}
|
2018-09-05 01:03:06 +02:00
|
|
|
evenParity = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
BinaryOctet::BinaryOctet(int x) {
|
|
|
|
for (int i = 0; i <= bitsPerOctet - 1; i++) {
|
|
|
|
int tmp = ((x >> i) & 1);
|
|
|
|
bitsAsDigits[i] = (tmp == 1) ? '1' : '0';
|
2018-06-02 10:50:37 +02:00
|
|
|
}
|
2018-09-05 01:03:06 +02:00
|
|
|
//TODO: das sollte eigentlich nicht benötigt werden
|
|
|
|
std::reverse(std::begin(bitsAsDigits), std::end(bitsAsDigits));
|
|
|
|
|
|
|
|
//einsen zählen
|
2018-06-02 10:50:37 +02:00
|
|
|
int i = 0;
|
|
|
|
for (char c : bitsAsDigits) {
|
|
|
|
if (c == '1') i++;
|
|
|
|
}
|
2018-09-05 01:03:06 +02:00
|
|
|
|
2018-06-02 10:50:37 +02:00
|
|
|
evenParity = i % 2 == 0;
|
2018-08-29 15:44:59 +02:00
|
|
|
}
|
2018-06-02 10:50:37 +02:00
|
|
|
|
2018-09-05 01:03:06 +02:00
|
|
|
|
2018-08-29 15:44:59 +02:00
|
|
|
bool operator!=(BinaryOctet a, BinaryOctet b) {
|
2018-09-05 01:03:06 +02:00
|
|
|
if (a.evenParity != b.evenParity) return false;
|
2018-08-29 15:44:59 +02:00
|
|
|
for (int i = 0; i < bitsPerOctet; ++i) {
|
2018-09-05 01:03:06 +02:00
|
|
|
if (a.bitsAsDigits[i] != b.bitsAsDigits[i]) return false;
|
2018-08-29 15:44:59 +02:00
|
|
|
}
|
|
|
|
return true;
|
2018-06-02 10:50:37 +02:00
|
|
|
}
|
|
|
|
|
2018-08-31 18:26:38 +02:00
|
|
|
const BinaryOctet BinaryOctet::operator--(const int) {
|
2018-08-31 14:05:18 +02:00
|
|
|
|
2018-06-02 10:50:37 +02:00
|
|
|
|
2018-08-31 18:26:38 +02:00
|
|
|
return *this;
|
2018-06-02 10:50:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BinaryOctet operator+(BinaryOctet a, BinaryOctet b) {
|
2018-08-29 15:44:59 +02:00
|
|
|
BinaryOctet result;
|
|
|
|
|
|
|
|
for (int i = 0; i < bitsPerOctet; ++i) {
|
2018-09-05 01:03:06 +02:00
|
|
|
if (a.bitsAsDigits[i] == b.bitsAsDigits[i]) result.bitsAsDigits[i] = '0';
|
2018-08-29 15:44:59 +02:00
|
|
|
else result.bitsAsDigits[i] = '1';
|
|
|
|
}
|
2018-06-02 10:50:37 +02:00
|
|
|
|
2018-08-29 15:44:59 +02:00
|
|
|
return result;
|
2018-06-02 10:50:37 +02:00
|
|
|
}
|
|
|
|
|
2018-08-31 18:26:38 +02:00
|
|
|
BinaryOctet operator+(BinaryOctet a, int b) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-06-02 10:50:37 +02:00
|
|
|
BinaryOctet operator/(BinaryOctet a, BinaryOctet b) {
|
|
|
|
|
2018-08-31 18:26:38 +02:00
|
|
|
return a;
|
2018-03-09 09:47:53 +01:00
|
|
|
}
|
|
|
|
|
2018-09-05 01:03:06 +02:00
|
|
|
BinaryOctet &BinaryOctet::operator=(int const &a) {
|
|
|
|
std::string tmp = std::to_string(a);
|
|
|
|
for (int i = 0; i >= tmp.length(); i++) {
|
2018-08-31 14:05:18 +02:00
|
|
|
bitsAsDigits[i] = tmp[i];
|
|
|
|
}
|
2018-08-29 15:44:59 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-06-02 10:50:37 +02:00
|
|
|
BinaryOctet doCalculation(BinaryOctet a, BinaryOctet b) {
|
|
|
|
BinaryOctet result;
|
2018-03-09 09:47:53 +01:00
|
|
|
|
2018-06-02 10:50:37 +02:00
|
|
|
for (; a != b; b--) {
|
|
|
|
a = a + 1;
|
|
|
|
a = a / b;
|
|
|
|
}
|
|
|
|
result = a + b;
|
|
|
|
return result;
|
2018-03-09 09:47:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// for println();
|
2018-06-02 10:50:37 +02:00
|
|
|
std::string as_string(BinaryOctet a) {
|
|
|
|
std::string result = "(";
|
|
|
|
for (char c : a.bitsAsDigits) {
|
|
|
|
result += c;
|
|
|
|
}
|
|
|
|
result += ")";
|
|
|
|
return result;
|
2018-03-09 09:47:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// for std::cout
|
2018-09-05 01:03:06 +02:00
|
|
|
std::ostream &operator<<(std::ostream &os, const BinaryOctet &toBePrinted) {
|
|
|
|
os << as_string(toBePrinted);
|
|
|
|
return os;
|
2018-08-29 15:44:59 +02:00
|
|
|
}
|
2018-03-09 09:47:53 +01:00
|
|
|
|
2018-06-02 10:50:37 +02:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
BinaryOctet a = 0b00001111;
|
2018-08-31 14:05:18 +02:00
|
|
|
std::cout << a << std::endl;
|
2018-08-31 18:26:38 +02:00
|
|
|
BinaryOctet b = 0b00000110;
|
|
|
|
std::cout << b << std::endl;
|
2018-08-29 15:44:59 +02:00
|
|
|
std::cout << new BinaryOctet(5) << std::endl;
|
|
|
|
std::cout << a + b << std::endl;
|
2018-06-02 10:50:37 +02:00
|
|
|
//println("result = ", doCalculation(a,b));
|
2018-09-05 01:03:06 +02:00
|
|
|
std::cout << "result = " << doCalculation(a, b) << std::endl;
|
2018-06-02 10:50:37 +02:00
|
|
|
return 0;
|
2018-03-09 09:47:53 +01:00
|
|
|
}
|