<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Dezimal in Binaerumrechner - Frage zum Programm]]></title><description><![CDATA[<p>Also ich bin gerade dabei mir C++ beizubringen und wollte ein Programm schreiben, das Dezimalzahlen in Binärzahlen umwandelt. Da ich alleine nicht weitergekommen bin, habe ich im Internet geguckt und bin dabei auf folgenden Code gestossen:</p>
<pre><code>#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;math.h&gt;

using namespace std;

int PrintBin(int i);

int main()
{
    int iEingabe = 0;

    cout &lt;&lt; &quot;Geben Sie eine Zahl ein, welche konvertiert werden soll:&quot; &lt;&lt; endl;
    cin &gt;&gt; iEingabe;
    cout &lt;&lt; &quot;Zahl in Binaer: &quot; &lt;&lt; endl;
    PrintBin(iEingabe);
    cout &lt;&lt; endl;
    system(&quot;pause&quot;);
    return 0;
}

int PrintBin(int i)
{
    if (i!=0) {
        PrintBin(i/2);
        if ((i % 2) == 0) {
            cout &lt;&lt; &quot;0&quot;;
        }
        else {
            cout &lt;&lt; &quot;1&quot;;
        }
    }
}
</code></pre>
<p>Das Programm funktioniert auch super, aber ehrlich gesagt frage ich mich WARUM <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /> Also wenn man jetzt als Beispiel die Zahl 23 nimmt, dann kommt ja als erster Rest 1 raus. Aber wieso bricht die Funktion an der Stelle nicht ab, sondern &quot;weiß&quot;, wann der Binärcode zuende ist? Ich hätte zum Beispiel eher daran gedacht, das Problem mit einer while-Schleife zu lösen, die Werte in einem Array zu speichern und diesen dann rückwärts auszugeben. Ist wahrscheinlich ein ganz blöder Denkfehler, aber ich steh grad echt aufm Schlauch. Danke schonmal im Voraus.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/308894/dezimal-in-binaerumrechner-frage-zum-programm</link><generator>RSS for Node</generator><lastBuildDate>Wed, 05 Aug 2026 14:30:42 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/308894.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 08 Oct 2012 20:43:43 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Dezimal in Binaerumrechner - Frage zum Programm on Mon, 08 Oct 2012 20:43:43 GMT]]></title><description><![CDATA[<p>Also ich bin gerade dabei mir C++ beizubringen und wollte ein Programm schreiben, das Dezimalzahlen in Binärzahlen umwandelt. Da ich alleine nicht weitergekommen bin, habe ich im Internet geguckt und bin dabei auf folgenden Code gestossen:</p>
<pre><code>#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;math.h&gt;

using namespace std;

int PrintBin(int i);

int main()
{
    int iEingabe = 0;

    cout &lt;&lt; &quot;Geben Sie eine Zahl ein, welche konvertiert werden soll:&quot; &lt;&lt; endl;
    cin &gt;&gt; iEingabe;
    cout &lt;&lt; &quot;Zahl in Binaer: &quot; &lt;&lt; endl;
    PrintBin(iEingabe);
    cout &lt;&lt; endl;
    system(&quot;pause&quot;);
    return 0;
}

int PrintBin(int i)
{
    if (i!=0) {
        PrintBin(i/2);
        if ((i % 2) == 0) {
            cout &lt;&lt; &quot;0&quot;;
        }
        else {
            cout &lt;&lt; &quot;1&quot;;
        }
    }
}
</code></pre>
<p>Das Programm funktioniert auch super, aber ehrlich gesagt frage ich mich WARUM <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /> Also wenn man jetzt als Beispiel die Zahl 23 nimmt, dann kommt ja als erster Rest 1 raus. Aber wieso bricht die Funktion an der Stelle nicht ab, sondern &quot;weiß&quot;, wann der Binärcode zuende ist? Ich hätte zum Beispiel eher daran gedacht, das Problem mit einer while-Schleife zu lösen, die Werte in einem Array zu speichern und diesen dann rückwärts auszugeben. Ist wahrscheinlich ein ganz blöder Denkfehler, aber ich steh grad echt aufm Schlauch. Danke schonmal im Voraus.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2258305</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2258305</guid><dc:creator><![CDATA[bmxfreak]]></dc:creator><pubDate>Mon, 08 Oct 2012 20:43:43 GMT</pubDate></item><item><title><![CDATA[Reply to Dezimal in Binaerumrechner - Frage zum Programm on Mon, 08 Oct 2012 21:00:33 GMT]]></title><description><![CDATA[<pre><code>23:
printbin(23/2 = 11)
-&gt;11:
  printbin(11/2 = 5)
--&gt;5:
   printbin(5/2 = 2)
---&gt;2:
    printbin(2/2 = 1)
----&gt;1:
     printbin(1/2 = 0)
-----&gt;0:
      Abbruch weil nicht i!=0
     1%2 == 1
     Ausgabe 1
   2%2 == 0
   Ausgabe 0
  5%2 == 1
  Ausgabe 1
 11%2 == 1
 Ausgabe 1
23%2 == 1
Ausgabe 1
</code></pre>
<p>Gesamtausgabe 10111. Passt.</p>
<p>Analog im Dezimalsystem, da kann man es besser nachvollziehen. Eingabe 512:</p>
<pre><code>512:
512/10 = 51
 51:
 51/10 = 5
  5:
  5/10 = 0
   0 -&gt; Keine Ausgabe
  5%10 = 5 -&gt; Ausgabe 5
 51%10 = 1 -&gt; Ausgabe 1
512%10 = 2 -&gt; Ausgabe 2
Gesamt: 512
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2258309</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2258309</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 08 Oct 2012 21:00:33 GMT</pubDate></item><item><title><![CDATA[Reply to Dezimal in Binaerumrechner - Frage zum Programm on Mon, 08 Oct 2012 21:03:56 GMT]]></title><description><![CDATA[<p>Cool, vielen Dank! Hab das i!=0 irgendwie außer Acht gelassen, aber jetzt ist es natürlich logisch!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2258312</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2258312</guid><dc:creator><![CDATA[bmxfreak]]></dc:creator><pubDate>Mon, 08 Oct 2012 21:03:56 GMT</pubDate></item></channel></rss>