2018-03-09 09:47:53 +01:00
|
|
|
#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-09-30 17:34:48 +02:00
|
|
|
bool evenParity;
|
|
|
|
char bitsAsDigits[bitsPerOctet]{};
|
2018-10-04 15:49:35 +02:00
|
|
|
|
2018-09-30 17:34:48 +02:00
|
|
|
BinaryOctet(int x = 0);
|
|
|
|
BinaryOctet(const BinaryOctet&) = default;
|
2018-10-04 15:49:35 +02:00
|
|
|
const BinaryOctet operator--(int);
|
|
|
|
const BinaryOctet operator++(int);
|
|
|
|
bool operator!=(BinaryOctet &a);
|
|
|
|
bool operator>=(BinaryOctet &a);
|
|
|
|
BinaryOctet operator-(BinaryOctet a);
|
2018-09-30 17:34:48 +02:00
|
|
|
BinaryOctet operator+(BinaryOctet a);
|
|
|
|
BinaryOctet operator/(BinaryOctet &a);
|
2018-03-09 09:47:53 +01:00
|
|
|
};
|
|
|
|
|
2018-09-05 01:03:06 +02:00
|
|
|
BinaryOctet::BinaryOctet(int x) {
|
2018-09-28 10:24:55 +02:00
|
|
|
for (char &bitsAsDigit : bitsAsDigits) {
|
|
|
|
bitsAsDigit = '0';
|
|
|
|
}
|
2018-10-04 15:49:35 +02:00
|
|
|
|
|
|
|
int remainder;
|
|
|
|
int temp = 0;
|
|
|
|
while (x != 0) {
|
|
|
|
remainder = x % 2;
|
|
|
|
x /= 2;
|
|
|
|
bitsAsDigits[temp] = static_cast<char>(remainder + '0');
|
|
|
|
temp++;
|
2018-06-02 10:50:37 +02:00
|
|
|
}
|
2018-10-04 15:49:35 +02:00
|
|
|
|
2018-09-05 01:03:06 +02:00
|
|
|
std::reverse(std::begin(bitsAsDigits), std::end(bitsAsDigits));
|
|
|
|
|
2018-10-04 15:49:35 +02:00
|
|
|
int ones = 0;
|
2018-06-02 10:50:37 +02:00
|
|
|
for (char c : bitsAsDigits) {
|
2018-10-04 15:49:35 +02:00
|
|
|
if (c == '1') ones++;
|
2018-06-02 10:50:37 +02:00
|
|
|
}
|
2018-09-05 01:03:06 +02:00
|
|
|
|
2018-10-04 15:49:35 +02:00
|
|
|
evenParity = ones % 2 == 0;
|
2018-08-29 15:44:59 +02:00
|
|
|
}
|
2018-06-02 10:50:37 +02:00
|
|
|
|
2018-10-04 15:49:35 +02:00
|
|
|
const BinaryOctet BinaryOctet::operator--(int) {
|
2018-09-30 17:34:48 +02:00
|
|
|
return operator-(*new BinaryOctet(1));
|
2018-09-28 10:24:55 +02:00
|
|
|
}
|
2018-06-02 10:50:37 +02:00
|
|
|
|
2018-10-04 15:49:35 +02:00
|
|
|
const BinaryOctet BinaryOctet::operator++(int) {
|
|
|
|
return operator+(*new BinaryOctet(1));
|
2018-06-02 10:50:37 +02:00
|
|
|
}
|
|
|
|
|
2018-09-30 17:34:48 +02:00
|
|
|
BinaryOctet BinaryOctet::operator+(BinaryOctet a) {
|
2018-08-29 15:44:59 +02:00
|
|
|
BinaryOctet result;
|
2018-09-21 23:54:09 +02:00
|
|
|
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';
|
2018-08-29 15:44:59 +02:00
|
|
|
|
2018-06-02 10:50:37 +02:00
|
|
|
|
2018-09-21 23:54:09 +02:00
|
|
|
if(over) tmp = aI + bI + 1;
|
|
|
|
else tmp = aI + bI;
|
|
|
|
over = false;
|
2018-06-02 10:50:37 +02:00
|
|
|
|
2018-08-31 18:26:38 +02:00
|
|
|
|
2018-09-21 23:54:09 +02:00
|
|
|
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;
|
2018-08-31 18:26:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-04 15:49:35 +02:00
|
|
|
BinaryOctet BinaryOctet::operator-(BinaryOctet a) {
|
2018-09-21 23:54:09 +02:00
|
|
|
BinaryOctet result;
|
2018-10-04 15:49:35 +02:00
|
|
|
bool over = false;
|
|
|
|
for(int i = bitsPerOctet - 1; i >= 0; --i) {
|
|
|
|
int tmp;
|
|
|
|
int aI = a.bitsAsDigits[i] - '0';
|
|
|
|
int bI = bitsAsDigits[i] - '0';
|
2018-06-02 10:50:37 +02:00
|
|
|
|
2018-10-04 15:49:35 +02:00
|
|
|
if(over) tmp = bI - aI - 1;
|
|
|
|
else tmp = bI - aI;
|
|
|
|
over = false;
|
2018-09-30 17:34:48 +02:00
|
|
|
|
2018-10-04 15:49:35 +02:00
|
|
|
if(tmp < 0) {
|
|
|
|
over = true;
|
|
|
|
tmp = tmp + 1;
|
|
|
|
}
|
|
|
|
result.bitsAsDigits[i] = static_cast<char>(tmp + '0');
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2018-09-30 17:34:48 +02:00
|
|
|
|
2018-10-04 15:49:35 +02:00
|
|
|
BinaryOctet BinaryOctet::operator/(BinaryOctet &a) {
|
|
|
|
BinaryOctet result = *new BinaryOctet(0);
|
|
|
|
|
|
|
|
while(*this >= a) {
|
|
|
|
*this = *this - a;
|
|
|
|
result = result + 1;
|
|
|
|
std::cout << result.bitsAsDigits << std::endl;
|
|
|
|
}
|
2018-09-21 23:54:09 +02:00
|
|
|
return result;
|
2018-03-09 09:47:53 +01:00
|
|
|
}
|
|
|
|
|
2018-10-04 15:49:35 +02:00
|
|
|
bool BinaryOctet::operator!=(BinaryOctet &a) {
|
|
|
|
if (a.evenParity !=evenParity) return false;
|
2018-09-30 17:34:48 +02:00
|
|
|
for (int i = 0; i < bitsPerOctet; ++i) {
|
2018-10-04 15:49:35 +02:00
|
|
|
if (a.bitsAsDigits[i] != bitsAsDigits[i]) return false;
|
2018-08-31 14:05:18 +02:00
|
|
|
}
|
2018-09-30 17:34:48 +02:00
|
|
|
return true;
|
2018-08-29 15:44:59 +02:00
|
|
|
}
|
|
|
|
|
2018-10-04 15:49:35 +02:00
|
|
|
|
|
|
|
//TODO: this
|
|
|
|
bool BinaryOctet::operator>=(BinaryOctet &a) {
|
|
|
|
if(!operator!=(a)) return true;
|
|
|
|
|
|
|
|
int aI = 0;
|
|
|
|
for(int i = bitsPerOctet - 1; i >= 0; --i) {
|
|
|
|
aI =+ (a.bitsAsDigits[i] - '0') * 2 ^ i;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bI = 0;
|
|
|
|
for(int i = bitsPerOctet - 1; i >= 0; --i) {
|
|
|
|
bI += (bitsAsDigits[i] - '0') * 2 ^ i;
|
|
|
|
}
|
|
|
|
return bI >= aI;
|
|
|
|
}
|
|
|
|
|
2018-06-02 10:50:37 +02:00
|
|
|
BinaryOctet doCalculation(BinaryOctet a, BinaryOctet b) {
|
|
|
|
BinaryOctet result;
|
|
|
|
for (; a != b; b--) {
|
|
|
|
a = a + 1;
|
|
|
|
a = a / b;
|
|
|
|
}
|
|
|
|
result = a + b;
|
|
|
|
return result;
|
2018-03-09 09:47:53 +01:00
|
|
|
}
|
|
|
|
|
2018-09-21 23:54:09 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-09-21 23:54:09 +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-09-21 23:54:09 +02:00
|
|
|
|
|
|
|
std::ostream &operator<<(std::ostream &os, BinaryOctet *toBePrinted) {
|
|
|
|
os << as_string(toBePrinted);
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-02 10:50:37 +02:00
|
|
|
int main(int argc, char **argv) {
|
2018-09-28 10:24:55 +02:00
|
|
|
|
|
|
|
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;
|
2018-09-21 23:54:09 +02:00
|
|
|
auto *c = new BinaryOctet(5);
|
2018-09-28 10:24:55 +02:00
|
|
|
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;
|
|
|
|
|
2018-10-04 15:49:35 +02:00
|
|
|
std::cout << "========= operator/ ==========" << std::endl;
|
|
|
|
std::cout << "a / b = " << a / b << std::endl;
|
|
|
|
|
2018-09-28 10:24:55 +02:00
|
|
|
|
|
|
|
std::cout << "======== operator-- ============" << std::endl;
|
|
|
|
BinaryOctet d = 0b111111111;
|
|
|
|
d--;
|
2018-10-04 15:49:35 +02:00
|
|
|
std::cout << "11111111-- = " << d-- << std::endl;
|
2018-09-28 10:24:55 +02:00
|
|
|
|
|
|
|
std::cout << "============== doCalculation ================" << std::endl;
|
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-09-30 17:34:48 +02:00
|
|
|
}
|