04_UDEF_MP BinaryOctet weitere fehlerhafte Operatoren
This commit is contained in:
parent
9aa4d96928
commit
3f7f982922
|
@ -13,6 +13,7 @@ public:
|
|||
BinaryOctet(const BinaryOctet &) = default;
|
||||
BinaryOctet &operator=(int const &a);
|
||||
const BinaryOctet operator--(int);
|
||||
const BinaryOctet operator-(BinaryOctet a);
|
||||
const BinaryOctet operator+(BinaryOctet a);
|
||||
};
|
||||
|
||||
|
@ -25,6 +26,9 @@ BinaryOctet::BinaryOctet() {
|
|||
|
||||
|
||||
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';
|
||||
|
@ -53,16 +57,23 @@ bool operator!=(BinaryOctet a, BinaryOctet b) {
|
|||
|
||||
//TODO: this method
|
||||
const BinaryOctet BinaryOctet::operator--(const int) {
|
||||
bool over = false;
|
||||
for(int i = bitsPerOctet - 1; i >= 0; --i) {
|
||||
int tmp = bitsAsDigits[i] - '0';
|
||||
return operator-(1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
bitsAsDigits[i] = static_cast<char>(tmp + '0');
|
||||
//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;
|
||||
|
||||
return *this;
|
||||
result = *this + a;
|
||||
std::cout << result.evenParity << std::endl;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
@ -157,15 +168,28 @@ std::ostream &operator<<(std::ostream &os, BinaryOctet *toBePrinted) {
|
|||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
BinaryOctet a = 0b00001111;
|
||||
std::cout << a << std::endl;
|
||||
BinaryOctet b = 0b00000110;
|
||||
std::cout << b << std::endl;
|
||||
b--;
|
||||
std::cout << b << std::endl;
|
||||
|
||||
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 << std::endl;
|
||||
std::cout << a + b << std::endl;
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue