Inverse Darstellung der Binärzahl
-
Hallo,
ich habe ein grosses Problem, das mich den ganzen sonntag schon nervt, wahrscheinlich ist die Antwort trivial, aber eben.
Also ich muss ein Pgrm schreiben, das eine Dezimal zahl als Binärzahl ausgibt.
Ich habe es geschaft mit einer do-while schleife und mit division und modulo eine zahl zbsp. 4 nach 001 umzurechnen. Jetzt aber das Problem 001 ist ja verkehrt herum, ich muss ja 100 kriegen für 1*22+0*21+0*2^0.
Wie mach ich das, wenn ich nur if else while for do verwenden darf?
Habe es schon mit if (a==0) a=1; else a=0 versucht, aber dann ändere ich ja einfach die 1 zu 0 und umgekehrt, aber dies ist ja falsch.
Im Bsp 4, bekäme ich ja 110 und das wäre ja im Dezimalsystem 6
Bin völlig verwirrt,
Gruss Axel
-
ehm einfach in string packen und umdrehen
-
Darf eben keinen String benutzen, da wir dies in der Vorlesung noch nicht behandelt haben
-
ausgabe rekursiv machen, dann ist sie auch anders herum.
-
:p Klingt cool, nur weiss ich doch nicht wie man eine Ausgabe rekursiv macht, hast du irgendwo eine Anleitung dafür
(Programmiere erst die 4 Woche, ist alles sehr neu für mich, sorry wegen diesen doofen Fragen)
Alex
-
Wie ist denn dein Code bisher, vielleicht lässt sich die Schleife einfach umdrehen?
-
Also hier meine source
// Program: Dec2Bin.C
// Convert a decimal number into its binary representation.#include <iostream>
int main()
{
// Input
std::cout << "Type in a number you would like being converted into its binary representation. n =? ";
unsigned int n;
std::cin >> n;// Computation
std::cout << " The binary representation of " << n << " is ";
do {int a = n%2;
if (a==1)
a=0;
else
a=1; // does not work, e.g. 4 then 110, but this is 6 in decimalstd::cout << a;
n = n/2;} while (n > 0);
// Output
std::cout << " \n";
return 0;
}
-
Hm, ne, die Schleife kann man nicht rumdrehen
Wie wärs, wenn du annimmst, dass ein int 4*CHAR_BIT (ist eine Konstante) Bits hat und du von oben jedes Bit auf Gesetztheit prüfst?
"Bit gesetzt oder nicht" kannst du rausfinden, indem du eine "1" bitnr-mal nach links shiftest (mit "<<"), mit der int-Zahl ver-und-dst (mit "&") und das Ergebnis auf != 0 überprüfst.
-
Habe ich leider noch nie gehört und ich glaube nicht, dass dies erlaubt ist.
Die Dozenten wollen stets das wir die konstrukte verwenden, die wir in der vorlesung behandelt haben und davon war noch nicht die rede, sorry
-
// Convert a decimal number into its binary representation. #include <iostream> void bin (unsigned v) { if (v/2) bin (v/2); putchar ('0' + (v&1)); } int main() { std::cout << "Type in a number you would like being converted into its binary representation. n =? "; unsigned int n; std::cin >> n; bin (n); }
-
Axel A schrieb:
Habe ich leider noch nie gehört und ich glaube nicht, dass dies erlaubt ist.
Die Dozenten wollen stets das wir die konstrukte verwenden, die wir in der vorlesung behandelt haben und davon war noch nicht die rede, sorry
Wovon war denn bereits die Rede? Die kürzeste Version, die mir einfällt, wäre über ein bitset<> - die nächstbessere Version wäre der Ansatz von Badestrand:
for(int p=sizeof(int)*CHAR_BIT-1;p>=0;--p) cout<<(val&(1<<p)?1:0;
-
For other Forum users still looking for a simple answer!
just make a sum instead of the seperate Output.
try this:
#include <iostream>
int main()
{
// input
int n, x, y;
int sum = 0;
std::cout << "enter a natural number n to convert in binary digits:\nn = ";
std::cin >> n;
x = n;// computation of the number n to binary digits in reverse order
while (x>0)
{
y = x%2;
sum = sum*10 + y;
x = x/2;
}// output
std::cout<<sum<<" is the binary of " << n << ".\n";return 0;
}
-
Thanks for the necromancy
.
I do not like your solution. It mixes two different concepts into one: values and representations. This does allow you some simplifications in your code but from experience I would say, that these simplifications will lead to problems later, because it is just plain wrong (from a mathematical standpoint) to mix these concepts.
You want to compute the representation (here: a sequence of digits, only using '1' and '0') of a value. But for doing so, you save it as a value-type, an integer. Later, you compute another representation of this new value, this time using the digits '0' to '9'. You may not have noticed this, but this is exactly what happens when you usestd::cout<<sum
. You have carefully chosen the intermediate value so that only the digits '0' and '1' appear in this final representation, but nonetheless, there are several immediate drawbacks with this technique:
1. You are doing the same calculations twice. First with your own code, second by using the standard library for number output. If you do it like this, you could instead have used a library in the first place.
2. This depends critically on the behavior of the ostream. Those things have an internal state. Using something likestd::cout << std::hex;
earlier in the program would break your code in a way, that is very hard to debug, as cout is a global variable that could be modified anywhere, any time. There are many more ways modify the details of numerical output, not just std::hex.
3. The supported range is rather low. On a usual PC, your code supports at most 10 binary digits. That's only values up to 1023!
4. It is hard to use your "representation" because it is not a true representation. If you wanted to use the representation in your program, not just print it to stdout, what would you use? sum? That is an integer value and it is not immediately apparent how it connects to the desired representation. Even if you know what to do, you need to do the same calculations all over again, see point 1, and could have just skipped the intermediate step. If you put your result into a string (for example by using a stringstream instead of stdout, which would also eliminate the problems with the global state of cout), it is again point 1: You could have done this in the first place.