2018-03-09 09:47:53 +01:00
|
|
|
// file main_04_UDEF_e.cpp
|
|
|
|
#include "../helpers/println.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
|
2018-06-02 10:50:37 +02:00
|
|
|
const int bitsPerOctet = 8;
|
2018-03-09 09:47:53 +01:00
|
|
|
|
|
|
|
struct BinaryOctet {
|
2018-06-02 10:50:37 +02:00
|
|
|
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(const BinaryOctet &) = default;
|
2018-03-09 09:47:53 +01:00
|
|
|
};
|
|
|
|
|
2018-06-02 10:50:37 +02:00
|
|
|
BinaryOctet::BinaryOctet(int x) {
|
|
|
|
int length = 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(BinaryOctet a, BinaryOctet b) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
BinaryOctet operator--(BinaryOctet &id, int) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
BinaryOctet operator+(BinaryOctet a, BinaryOctet b) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
BinaryOctet operator/(BinaryOctet a, BinaryOctet b) {
|
|
|
|
|
2018-03-09 09:47:53 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
/*std::ostream& operator<< (std::ostream& os, const BinaryOctet &toBePrinted){
|
|
|
|
os << "(" << ")";
|
|
|
|
return os;
|
|
|
|
}*/
|
|
|
|
|
2018-06-02 10:50:37 +02:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
BinaryOctet a = 0b00001111;
|
|
|
|
BinaryOctet b = 0b00000110;
|
|
|
|
println(new BinaryOctet(5));
|
|
|
|
println(new BinaryOctet(5));
|
|
|
|
//println("result = ", doCalculation(a,b));
|
|
|
|
//std::cout << "result = " << doCalculation(a,b) << std::endl;
|
|
|
|
return 0;
|
2018-03-09 09:47:53 +01:00
|
|
|
}
|