<?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[Bit, Byte und Bitverschiebung]]></title><description><![CDATA[<p>Hallo liebe Community!</p>
<p>Ich arbeite gerade an dem Buch &quot;Der C++ Programmierer&quot;!<br />
Bei dem Übungsbeispiel 1.5 steh ich etwas auf der Leitung. Ich hab mir bereits Gedanken dazu gemacht, bin aber zu keiner vernünftigen Lösung kommen!</p>
<pre><code>#include &lt;iostream&gt;
using namespace std;

int main() {
  cout &lt;&lt; &quot;Eingabe einer Zahl: &quot;;
  int zahl = 0;
  cin &gt;&gt; zahl;
  int anzahlDerBytes = sizeof zahl;
  int anzahlDerBits = 8 * anzahlDerBytes;
  cout &lt;&lt; &quot; binär :  &quot;;
  for (int k = anzahlDerBits - 1; k &gt;= 0; --k) {
    if (zahl &amp; (1 &lt;&lt; k)) {
      cout &lt;&lt; '1';
    } else {
      cout &lt;&lt; '0';
    }
  }
  cout &lt;&lt; '\n';
}
</code></pre>
<p>Meine Fragen:<br />
Warum muss ich in der for Schleife die Variabel &quot;anzahlDerBits&quot; um 1 subtrahieren?<br />
Warum verschiebe ich in der if Anweisung das k bitweise um 1 nach links?</p>
<p>Vielen Dank schon mal im vorhinein</p>
<p>lg Brail</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/336658/bit-byte-und-bitverschiebung</link><generator>RSS for Node</generator><lastBuildDate>Sun, 19 Apr 2026 00:03:05 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/336658.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 08 Feb 2016 15:39:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Bit, Byte und Bitverschiebung on Mon, 08 Feb 2016 15:39:01 GMT]]></title><description><![CDATA[<p>Hallo liebe Community!</p>
<p>Ich arbeite gerade an dem Buch &quot;Der C++ Programmierer&quot;!<br />
Bei dem Übungsbeispiel 1.5 steh ich etwas auf der Leitung. Ich hab mir bereits Gedanken dazu gemacht, bin aber zu keiner vernünftigen Lösung kommen!</p>
<pre><code>#include &lt;iostream&gt;
using namespace std;

int main() {
  cout &lt;&lt; &quot;Eingabe einer Zahl: &quot;;
  int zahl = 0;
  cin &gt;&gt; zahl;
  int anzahlDerBytes = sizeof zahl;
  int anzahlDerBits = 8 * anzahlDerBytes;
  cout &lt;&lt; &quot; binär :  &quot;;
  for (int k = anzahlDerBits - 1; k &gt;= 0; --k) {
    if (zahl &amp; (1 &lt;&lt; k)) {
      cout &lt;&lt; '1';
    } else {
      cout &lt;&lt; '0';
    }
  }
  cout &lt;&lt; '\n';
}
</code></pre>
<p>Meine Fragen:<br />
Warum muss ich in der for Schleife die Variabel &quot;anzahlDerBits&quot; um 1 subtrahieren?<br />
Warum verschiebe ich in der if Anweisung das k bitweise um 1 nach links?</p>
<p>Vielen Dank schon mal im vorhinein</p>
<p>lg Brail</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2486282</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2486282</guid><dc:creator><![CDATA[Brail01]]></dc:creator><pubDate>Mon, 08 Feb 2016 15:39:01 GMT</pubDate></item><item><title><![CDATA[Reply to Bit, Byte und Bitverschiebung on Mon, 08 Feb 2016 15:52:26 GMT]]></title><description><![CDATA[<p>1. Weil die Bits einer (zum Beispiel) 32 - Bit Zahl durchnumeriert werden von 0 bis 31<br />
2. Das tust Du nicht. Du verschiebst in den Klammern 1 Bit um k Stellen nach links.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2486284</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2486284</guid><dc:creator><![CDATA[Belli]]></dc:creator><pubDate>Mon, 08 Feb 2016 15:52:26 GMT</pubDate></item><item><title><![CDATA[Reply to Bit, Byte und Bitverschiebung on Mon, 08 Feb 2016 15:56:28 GMT]]></title><description><![CDATA[<p>Brail01 schrieb:</p>
<blockquote>
<p>Warum verschiebe ich in der if Anweisung das k bitweise um 1 nach links?</p>
</blockquote>
<p>Du verschiebst die 1 um k Positionen. Mit k=3 gibt das 00001000.</p>
<p>Brail01 schrieb:</p>
<blockquote>
<p>Warum muss ich in der for Schleife die Variabel &quot;anzahlDerBits&quot; um 1 subtrahieren?</p>
</blockquote>
<p>0-mal schieben = erste Position<br />
...<br />
n-1-mal schieben = n-te Position</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2486285</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2486285</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Mon, 08 Feb 2016 15:56:28 GMT</pubDate></item><item><title><![CDATA[Reply to Bit, Byte und Bitverschiebung on Mon, 08 Feb 2016 16:07:11 GMT]]></title><description><![CDATA[<p>Vielen Dank für die super schnellen und vorallem hilfreiche Antworten!<br />
Hab das jetzt tatsächlich verstanden! <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="😃"
    /></p>
<p>lg Brail</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2486286</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2486286</guid><dc:creator><![CDATA[Brail]]></dc:creator><pubDate>Mon, 08 Feb 2016 16:07:11 GMT</pubDate></item></channel></rss>