<?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[Binaere Darstellung einer Zahl]]></title><description><![CDATA[<p>Ich will eine float Variable als eine Reihe von 0 und 1 darstellen. Deshalb wird ein char Zeiger benutzt. Trotzdem weiß ich nicht wie es weiter gehen muss.</p>
<pre><code class="language-cpp">float p= 3.14;
char *pf= reinterpret_cast&lt;char*&gt;(&amp;p);
</code></pre>
<p>Die einzelnen Bits sollen in einem vector&lt;bool&gt; gespeichert werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/147759/binaere-darstellung-einer-zahl</link><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Sep 2026 04:33:09 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/147759.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 19 May 2006 16:34:34 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Binaere Darstellung einer Zahl on Fri, 19 May 2006 16:34:34 GMT]]></title><description><![CDATA[<p>Ich will eine float Variable als eine Reihe von 0 und 1 darstellen. Deshalb wird ein char Zeiger benutzt. Trotzdem weiß ich nicht wie es weiter gehen muss.</p>
<pre><code class="language-cpp">float p= 3.14;
char *pf= reinterpret_cast&lt;char*&gt;(&amp;p);
</code></pre>
<p>Die einzelnen Bits sollen in einem vector&lt;bool&gt; gespeichert werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1061133</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1061133</guid><dc:creator><![CDATA[khalderon]]></dc:creator><pubDate>Fri, 19 May 2006 16:34:34 GMT</pubDate></item><item><title><![CDATA[Reply to Binaere Darstellung einer Zahl on Fri, 19 May 2006 16:43:54 GMT]]></title><description><![CDATA[<p>Da stellt sich doch als erstes die Frage: welche Darstellung soll gewählt werden?</p>
<p>Ehrlich gesagt weiß ich nicht wie ein aktueller Computer eine Kommazahl speichert, aber ich habe in meinem ersten Semester mehrere Methoden kennengelernt wie das zu bewerkstelligen ist.<br />
Eine für Festkommadarstellung, und ein paar für Gleitkommadarstellung.<br />
Glaube der IEEE Standard mit der benötigten Genauigkeit wird genutzt (float = 32bit). Da hast du dann ein Bit fürs Vorzeichen, 8 für den Exponent und 23 für die Mantisse...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1061145</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1061145</guid><dc:creator><![CDATA[Tobias W]]></dc:creator><pubDate>Fri, 19 May 2006 16:43:54 GMT</pubDate></item><item><title><![CDATA[Reply to Binaere Darstellung einer Zahl on Fri, 19 May 2006 16:48:10 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">template&lt;class Inserter, class Type&gt;
void push_binary(Type const&amp; value, Inserter insert) 
{
    for(int byte = 0; byte &lt; sizeof(Type); ++byte) {
        for(int bit = 0; bit &lt; 8; ++bit) {
            insert = (reinterpret_cast&lt;unsigned char*&gt;(&amp;value)[byte] &amp; (1 &lt;&lt; bit)) &gt; 0 ? 1 : 0;
        }
    }
}

vector&lt;bool&gt; vec;
push_back(3.14f, back_insterter&lt;vector&lt;bool&gt; &gt;(vec));
</code></pre>
<p>Alles ungetestet.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1061150</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1061150</guid><dc:creator><![CDATA[Fachman für Fragen]]></dc:creator><pubDate>Fri, 19 May 2006 16:48:10 GMT</pubDate></item><item><title><![CDATA[Reply to Binaere Darstellung einer Zahl on Fri, 19 May 2006 17:07:23 GMT]]></title><description><![CDATA[<p>Warn paar Fehler drin, Schande über mich, aber habs schnell getestet da ich hier (Arbeit) eh nix zu tun habe und ich noch nicht gehen kann (will ja Lohn).</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;iterator&gt;
#include &lt;algorithm&gt;

template&lt;class Inserter, class Type&gt;
void push_binary(Type const&amp; value, Inserter insert)
{
    for(int byte = 0; byte &lt; sizeof(Type); ++byte) {
        for(int bit = 0; bit &lt; 8; ++bit) {
            insert = (reinterpret_cast&lt;const unsigned char*&gt;(&amp;value)[byte]
                     &amp; (1 &lt;&lt; bit)) &gt; 0 ? 1 : 0;
        }
    }
}

int main()
{
    using namespace std;

    vector&lt;bool&gt; vec;
    vec.reserve(sizeof(float) * 8);
    push_binary(3.14f, back_inserter&lt;vector&lt;bool&gt; &gt;(vec));
    copy(vec.begin(), vec.end(), ostream_iterator&lt;bool&gt;(cout, &quot;\n&quot;));

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1061160</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1061160</guid><dc:creator><![CDATA[Fachman für Fragen]]></dc:creator><pubDate>Fri, 19 May 2006 17:07:23 GMT</pubDate></item><item><title><![CDATA[Reply to Binaere Darstellung einer Zahl on Fri, 19 May 2006 20:03:17 GMT]]></title><description><![CDATA[<p>Fachman für Fragen, danke sehr. Die Darstellung der Zahlen sollte eigentlich 1:1 sein - d.h. wir haben 3.14 als float Variable -&gt; diese Variable besteht aus mehreren Bytes -&gt; die Bytes werden in Bits zerlegt -&gt; die Bits werden gespeichert. Wie Sie sehen, spielt die Darstellung in diesem Fall keine große Rolle. Und den Vorschlag mit STL finde ich einfach toll - jetzt kann ich sogar eigene Objekte in Bits verwandeln <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1061252</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1061252</guid><dc:creator><![CDATA[khalderon]]></dc:creator><pubDate>Fri, 19 May 2006 20:03:17 GMT</pubDate></item><item><title><![CDATA[Reply to Binaere Darstellung einer Zahl on Fri, 02 Jun 2006 13:27:47 GMT]]></title><description><![CDATA[<p>Könnten Sie mir jetzt bei der Umkehrimplementation helfen. Wir haben die Bits in einem sequentiellen Feld, und wir brauchen das Objekt!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1070106</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1070106</guid><dc:creator><![CDATA[khalderon]]></dc:creator><pubDate>Fri, 02 Jun 2006 13:27:47 GMT</pubDate></item><item><title><![CDATA[Reply to Binaere Darstellung einer Zahl on Fri, 02 Jun 2006 16:30:07 GMT]]></title><description><![CDATA[<p>Am Rande möchte ich ein kleines Metaprogramm vorstellen, das Zahlen im Binärformat zu Zeiten des Kompilierens ins Dezimalformat umwandelt. Als mögliche Lösung zu einer der Aufgaben (3-0) aus &quot;C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond' von D. Abrahams und A. Gurtovoy habe ich es wie folgt geschrieben. Eine sehr ähnliche Möglichkeit findet sich auf dem Zugehlörigen Wiki unter</p>
<p><a href="http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?CPPTM_Answers_-_Exercise_3-0" rel="nofollow">http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?CPPTM_Answers_-_Exercise_3-0</a></p>
<p>Die Überprüfung mit dem BOOST_STATIC_ASSERT habe ich dort heraus übernommen.</p>
<pre><code class="language-cpp">#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;boost/static_assert.hpp&gt;

using namespace std;

template &lt;unsigned int n&gt; struct binary
{
  static const unsigned int d = n % 10;
  BOOST_STATIC_ASSERT(d == 0 || d == 1);
  static const unsigned int value = d + (binary&lt;n/10&gt;::value * 2);
};

template &lt;&gt; struct binary&lt;0&gt;
{
  static const unsigned int value = 0;
};

int main()
{
  cout &lt;&lt; binary&lt;11001&gt;::value &lt;&lt; endl;
  cout &lt;&lt; binary&lt;110111&gt;::value &lt;&lt; endl;
  cout &lt;&lt; binary&lt;1111011&gt;::value &lt;&lt; endl;

  /**
   * Ausgabe: 25 55 123
   */

  return EXIT_SUCCESS;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1070190</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1070190</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Fri, 02 Jun 2006 16:30:07 GMT</pubDate></item><item><title><![CDATA[Reply to Binaere Darstellung einer Zahl on Sat, 03 Jun 2006 11:10:33 GMT]]></title><description><![CDATA[<p>Hmm, jetzt denke ich, dass vector&lt;bool&gt; nicht so effektiv ist. Außerdem sind die Daten, die ich umwandle immer ein Vielfach von 8 Bits. Deshalb schlage ich vor, dass anstatt vector&lt;bool&gt; - vector&lt;bitset&lt;8&gt; &gt; zu benutzen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1070606</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1070606</guid><dc:creator><![CDATA[khalderon]]></dc:creator><pubDate>Sat, 03 Jun 2006 11:10:33 GMT</pubDate></item></channel></rss>