1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | #include <stdio.h> #include <iostream> #include <cmath> using namespace std; /* zu Aufgabe 4a.1: Beschreibung: Funktion zur Umwandlung einer dezimalen Zahl in bin�re Darstellung. �bergabearameter: dez_zahl - die zu umwandelnde Dezimalzahl, bitanzahl - Anzahl der Bits in bin�rer Darstellung. bit_vektor - Ergebnisarray aus den Bitstellen der bin�ren Zahl. */ void dez_to_bin(int bit_vektor[], int dez_zahl, int bitanzahl) { // ******************************************************************************* // Die Funktion mit dem Inhalt f�llen if (dez_zahl < 0){ dez_zahl = std::pow(2, bitanzahl) + dez_zahl; } for(int i = 0; i < bitanzahl; i++){ bit_vektor[i] = dez_zahl % 2; //BigEndian (Most Significant Bit (MSB) at the end) dez_zahl /= 2; } /*Hier in LittleEndian for(int i = 0; i < bitanzahl; i++){ bit_vektor[bitanzahl - i - 1] = dez_zahl % 2; //LittleEndian (Least Significant Bit (MSB) at the end) dez_zahl /= 2; } */ } void halb_add(int ergebnis[], int bin_zahl1[], int bin_zahl2[], bool sum_carry) { if (sum_carry) { // f�r Summe for (int i = 0; i <= 31; i++) { ergebnis[i] = (bin_zahl1[i] ^ bin_zahl2[i]); } } else { //f�r carry for (int i = 0; i <= 31; i++) { ergebnis[i] = (bin_zahl1[i] & bin_zahl2[i]); } } } // Alternativ 1-Bit-Halbaddierer int halb_add_1bit(int bin_zahl1, int bin_zahl2, bool sum_carry) { if (sum_carry) { return (bin_zahl1 ^ bin_zahl2); } else { //f�r carry return (bin_zahl1 & bin_zahl2); } } /* zu Aufgabe 4a.2: Beschreibung: Gibt den Vektor auf den Bildschirm aus. (Wertigkeit der Bitstellen beachten!) �bergabeparameter: binzahl - Feld, der ausgegeben wird, anzahl_bits - Anzahl der Bitstellen */ void ausgabe(int binzahl[], int anzahl_bits) { // ******************************************************************************* // Diesen Bereich bitte mit geeigneter Funktionalit�t f�llen //iterieren über int array und ausgeben der einzelnen Stellen for(int i = 0; i < anzahl_bits; i++){ //print backwards 'cause binzahl is BigEndian and LittleEndian should be displayed cout << binzahl[anzahl_bits - 1 - i]; } // ******************************************************************************* cout << endl; } /* zu Aufgabe 4b.1: Beschreibung: Funktion beschreibt einen Volladdierer durch logische Gleichungen f�r Summe und �bertag. �bergabeparameter: bin_zahl1 - die zu addierende Bin�rzahl (als Feld), bin_zahl2 - die zu addierende Bin�rzahl (als Feld), sum_carry - Je nach Auswahl Berechnung der Summe oder des �bertrags. ergebnis - Ergebnisarray aus den Bitstellen der Summe / des �bertrags. */ void voll_add(int ergebnis[], int bin_zahl1[], int bin_zahl2[], bool sum_carry, int anzahl_bits) { //check for valid bit number if (anzahl_bits > 32 || anzahl_bits < 1){ cerr << "unexpected number of bits" << endl; return; } int c = 0; if(sum_carry){ for(int i = 0; i < anzahl_bits ; i++){ ergebnis[i] = ((bin_zahl1[i] ^ bin_zahl2[i]) ^ c); // c is carry c = ((bin_zahl1[i] & bin_zahl2[i]) | (bin_zahl1[i] & c)) | (bin_zahl2[i] & c); } }else { for(int i = 0; i < anzahl_bits ; i++){ c = ((bin_zahl1[i] & bin_zahl2[i]) | (bin_zahl1[i] & c)) | (bin_zahl2[i] & c); ergebnis[i] = c; } } } int main() { int dez_zahl1, dez_zahl2, anzahl_bits; int bin_zahl1[32] = { 0 }; int bin_zahl2[32] = { 0 }; int summe[32] = { 0 }; int carry[33] = { 0 }; cout << "Geben Sie 2 dezimale Zahlen ein:" << endl; cin >> dez_zahl1 >> dez_zahl2; cout << endl; cout << "Geben Sie Anzahl der Bits fuer binaere Darstellung ein:" << endl; cin >> anzahl_bits; // Konvertierung in Bin�rdarstellung dez_to_bin(bin_zahl1, dez_zahl1, anzahl_bits); dez_to_bin(bin_zahl2, dez_zahl2, anzahl_bits); // Ergebnisse f�r Halbaddierer cout << "\n\n========Halbaddierer========" << endl; cout << " "; ausgabe(bin_zahl1, anzahl_bits); cout << "+"; ausgabe(bin_zahl2, anzahl_bits); cout << "______________________" << endl; halb_add(summe, bin_zahl1, bin_zahl2, true); halb_add(carry, bin_zahl1, bin_zahl2, false); ausgabe(carry, anzahl_bits); cout << " "; ausgabe(summe, anzahl_bits); cout << endl; cout << "========Halbaddierer========" << endl; // Ergebnisse f�r Volladdierer cout << "\n\n========Volladdierer========" << endl; cout << " "; ausgabe(bin_zahl1, anzahl_bits); cout << "+"; ausgabe(bin_zahl2, anzahl_bits); cout << "______________________" << endl; voll_add(summe, bin_zahl1, bin_zahl2, true, anzahl_bits); voll_add(carry, bin_zahl1, bin_zahl2, false, anzahl_bits); ausgabe(carry, anzahl_bits); cout << " "; ausgabe(summe, anzahl_bits); cout << endl; system("pause"); return 0; } |
The entries are the properties of their respective owners.
Powered by Flask, SQLAlchemy, Pygments and Bootstrap.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.