04_UDEF_MP BinaryOctet Constructor fehlerfrei
This commit is contained in:
parent
08760f4afc
commit
3b1f7b9af3
|
@ -1,6 +1,6 @@
|
||||||
// file main_04_UDEF_e.cpp
|
// file main_04_UDEF_e.cpp
|
||||||
#include "../../helpers/println.hpp"
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
const int bitsPerOctet = 8;
|
const int bitsPerOctet = 8;
|
||||||
|
|
||||||
|
@ -8,31 +8,38 @@ struct BinaryOctet {
|
||||||
public:
|
public:
|
||||||
bool evenParity; // set to true if number of '1' in bitsAsDigits is even, false otherwise
|
bool evenParity; // set to true if number of '1' in bitsAsDigits is even, false otherwise
|
||||||
char bitsAsDigits[bitsPerOctet]{}; // bit values as chars
|
char bitsAsDigits[bitsPerOctet]{}; // bit values as chars
|
||||||
BinaryOctet(int x = 0);
|
BinaryOctet();
|
||||||
|
BinaryOctet(int x);
|
||||||
BinaryOctet(const BinaryOctet &) = default;
|
BinaryOctet(const BinaryOctet &) = default;
|
||||||
BinaryOctet &operator=(int const &a);
|
BinaryOctet &operator=(int const &a);
|
||||||
const BinaryOctet operator--(int);
|
const BinaryOctet operator--(int);
|
||||||
};
|
};
|
||||||
|
|
||||||
BinaryOctet::BinaryOctet(int x) {
|
BinaryOctet::BinaryOctet() {
|
||||||
//Alles mit 0 füllen
|
|
||||||
for (char &bitsAsDigit : bitsAsDigits) {
|
for (char &bitsAsDigit : bitsAsDigits) {
|
||||||
bitsAsDigit = '0';
|
bitsAsDigit = '0';
|
||||||
}
|
}
|
||||||
int length = static_cast<int>(std::to_string(x).length() * 4);
|
evenParity = true;
|
||||||
while (length >= 0) {
|
|
||||||
bitsAsDigits[length-1] = (x % 2 == 0) ? '0' : '1';
|
|
||||||
std::cout << x << " : " << length << " : " << bitsAsDigits[length-1] << std::endl;
|
|
||||||
x = x / 2;
|
|
||||||
length--;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BinaryOctet::BinaryOctet(int x) {
|
||||||
|
for (int i = 0; i <= bitsPerOctet - 1; i++) {
|
||||||
|
int tmp = ((x >> i) & 1);
|
||||||
|
bitsAsDigits[i] = (tmp == 1) ? '1' : '0';
|
||||||
|
}
|
||||||
|
//TODO: das sollte eigentlich nicht benötigt werden
|
||||||
|
std::reverse(std::begin(bitsAsDigits), std::end(bitsAsDigits));
|
||||||
|
|
||||||
|
//einsen zählen
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (char c : bitsAsDigits) {
|
for (char c : bitsAsDigits) {
|
||||||
if (c == '1') i++;
|
if (c == '1') i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
evenParity = i % 2 == 0;
|
evenParity = i % 2 == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool operator!=(BinaryOctet a, BinaryOctet b) {
|
bool operator!=(BinaryOctet a, BinaryOctet b) {
|
||||||
if (a.evenParity != b.evenParity) return false;
|
if (a.evenParity != b.evenParity) return false;
|
||||||
for (int i = 0; i < bitsPerOctet; ++i) {
|
for (int i = 0; i < bitsPerOctet; ++i) {
|
||||||
|
@ -68,9 +75,8 @@ BinaryOctet operator/(BinaryOctet a, BinaryOctet b) {
|
||||||
}
|
}
|
||||||
|
|
||||||
BinaryOctet &BinaryOctet::operator=(int const &a) {
|
BinaryOctet &BinaryOctet::operator=(int const &a) {
|
||||||
std::cout << a << std::endl;
|
std::string tmp = std::to_string(a);
|
||||||
std::string tmp = to_string(a);
|
for (int i = 0; i >= tmp.length(); i++) {
|
||||||
for(int i = 0; i < bitsPerOctet; i++) {
|
|
||||||
bitsAsDigits[i] = tmp[i];
|
bitsAsDigits[i] = tmp[i];
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
|
@ -109,7 +115,6 @@ int main(int argc, char **argv) {
|
||||||
BinaryOctet b = 0b00000110;
|
BinaryOctet b = 0b00000110;
|
||||||
std::cout << b << std::endl;
|
std::cout << b << std::endl;
|
||||||
std::cout << new BinaryOctet(5) << std::endl;
|
std::cout << new BinaryOctet(5) << std::endl;
|
||||||
println(a + b);
|
|
||||||
std::cout << a + b << std::endl;
|
std::cout << a + b << std::endl;
|
||||||
//println("result = ", doCalculation(a,b));
|
//println("result = ", doCalculation(a,b));
|
||||||
std::cout << "result = " << doCalculation(a, b) << std::endl;
|
std::cout << "result = " << doCalculation(a, b) << std::endl;
|
||||||
|
|
|
@ -24,7 +24,8 @@ int main() {
|
||||||
std::uniform_int_distribution<int> distribution(0, 23);
|
std::uniform_int_distribution<int> distribution(0, 23);
|
||||||
|
|
||||||
for(int i = 0; i <= 41; i++) {
|
for(int i = 0; i <= 41; i++) {
|
||||||
array[i] = distribution(generator);
|
int rnd = distribution(generator);
|
||||||
|
array[i] = rnd;
|
||||||
}
|
}
|
||||||
|
|
||||||
unittest_stringSimilarity();
|
unittest_stringSimilarity();
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
int main() {
|
int main() {
|
||||||
Derived * derived = new Derived();
|
Derived * derived = new Derived();
|
||||||
|
|
||||||
bar(derived);
|
foo(derived);
|
||||||
|
|
||||||
delete derived;
|
delete derived;
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ class Base {
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Derived : Base {
|
class Derived : public Base {
|
||||||
};
|
};
|
||||||
|
|
||||||
void foo(Base* base) {
|
void foo(Base* base) {
|
||||||
|
|
Loading…
Reference in New Issue