<?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[Frage zu Bitverschiebung]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich lerne momentan mit dem Buch &quot;Der C++ Programmierer&quot; von Ulrich Breymann.</p>
<p>Es wurde eine Übungsaufgabe gestellt, bei der man mithilfe einer Schleife eine eingegebene Zahl binär ausgeben soll.</p>
<p>Hier ist die vorgegebene Musterlösung:</p>
<blockquote>
<p>#include&lt;iostream&gt;<br />
using namespace std;<br />
int main() {<br />
cout &lt;&lt; &quot;Eingabe einer Zahl: &quot;;<br />
int zahl = 0;<br />
cin &gt;&gt; zahl;<br />
int anzahlDerBytes = sizeof zahl;<br />
int anzahlDerBits = 8  anzahlDerBytes;<br />
cout &lt;&lt;&quot; binär : &quot;;<br />
for(int k = anzahlDerBits–1; k &gt;= 0 ; ––k) {<br />
if(zahl &amp; (1 &lt;&lt; k)) {<br />
cout &lt;&lt; &quot;1&quot;;<br />
}<br />
else {<br />
cout &lt;&lt; &quot;0&quot;;<br />
}<br />
}<br />
cout &lt;&lt; endl;<br />
}</p>
</blockquote>
<p>Ich verstehe hierbei die if-Bedingung nicht.</p>
<p>Warum wird die Zahl 1 um k-Stellen verschoben und nicht die Variable zahl?</p>
<p>Desweiteren verstehe ich auch den Rest der if-Bedingung nicht.<br />
Was genau bedeutet (zahl &amp; (1 &lt;&lt; k)?</p>
<p>Ich hoffe ihr könnt mir weiterhelfen.<br />
Viele Grüße<br />
LieutenantTanaka</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/330171/frage-zu-bitverschiebung</link><generator>RSS for Node</generator><lastBuildDate>Fri, 03 Jul 2026 11:54:06 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/330171.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 25 Dec 2014 12:13:19 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Frage zu Bitverschiebung on Thu, 25 Dec 2014 12:13:19 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich lerne momentan mit dem Buch &quot;Der C++ Programmierer&quot; von Ulrich Breymann.</p>
<p>Es wurde eine Übungsaufgabe gestellt, bei der man mithilfe einer Schleife eine eingegebene Zahl binär ausgeben soll.</p>
<p>Hier ist die vorgegebene Musterlösung:</p>
<blockquote>
<p>#include&lt;iostream&gt;<br />
using namespace std;<br />
int main() {<br />
cout &lt;&lt; &quot;Eingabe einer Zahl: &quot;;<br />
int zahl = 0;<br />
cin &gt;&gt; zahl;<br />
int anzahlDerBytes = sizeof zahl;<br />
int anzahlDerBits = 8  anzahlDerBytes;<br />
cout &lt;&lt;&quot; binär : &quot;;<br />
for(int k = anzahlDerBits–1; k &gt;= 0 ; ––k) {<br />
if(zahl &amp; (1 &lt;&lt; k)) {<br />
cout &lt;&lt; &quot;1&quot;;<br />
}<br />
else {<br />
cout &lt;&lt; &quot;0&quot;;<br />
}<br />
}<br />
cout &lt;&lt; endl;<br />
}</p>
</blockquote>
<p>Ich verstehe hierbei die if-Bedingung nicht.</p>
<p>Warum wird die Zahl 1 um k-Stellen verschoben und nicht die Variable zahl?</p>
<p>Desweiteren verstehe ich auch den Rest der if-Bedingung nicht.<br />
Was genau bedeutet (zahl &amp; (1 &lt;&lt; k)?</p>
<p>Ich hoffe ihr könnt mir weiterhelfen.<br />
Viele Grüße<br />
LieutenantTanaka</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2434654</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2434654</guid><dc:creator><![CDATA[[[global:former_user]]]]></dc:creator><pubDate>Thu, 25 Dec 2014 12:13:19 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu Bitverschiebung on Thu, 25 Dec 2014 12:28:02 GMT]]></title><description><![CDATA[<p>Beachte, daß &amp; für das bitweise UND steht, es ist nicht das logische UND, was &amp;&amp; geschrieben wird.</p>
<p>LieutenantTanaka schrieb:</p>
<blockquote>
<p>Warum wird die Zahl 1 um k-Stellen verschoben und nicht die Variable zahl?</p>
</blockquote>
<p>Geschmachssache.</p>
<pre><code>if ((zahl &amp; (1 &lt;&lt; k)) == (1 &lt;&lt; k))
</code></pre>
<p>und</p>
<pre><code>if (((zahl &gt;&gt; k) &amp; 1) == 1)
</code></pre>
<p>testen das gleiche, nämlich ob das Bit an Stelle k (von rechts aus gezählt) gesetzt ist.</p>
<p>LieutenantTanaka schrieb:</p>
<blockquote>
<p>Desweiteren verstehe ich auch den Rest der if-Bedingung nicht.<br />
Was genau bedeutet (zahl &amp; (1 &lt;&lt; k)?</p>
</blockquote>
<p>Naja, es ist da egal, ob man schreibt</p>
<pre><code>if ((zahl &amp; (1 &lt;&lt; k)) == (1 &lt;&lt; k))
</code></pre>
<p>oder</p>
<pre><code>if ((zahl &amp; (1 &lt;&lt; k)) != 0)
</code></pre>
<p>udn ein !=0 braucht man im if nicht, also geht auch</p>
<pre><code>if ((zahl &amp; (1 &lt;&lt; k))   )
</code></pre>
<p>also</p>
<pre><code>if (zahl &amp; (1 &lt;&lt; k))
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2434656</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2434656</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Thu, 25 Dec 2014 12:28:02 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu Bitverschiebung on Thu, 25 Dec 2014 12:36:39 GMT]]></title><description><![CDATA[<p>LieutenantTanaka schrieb:</p>
<blockquote>
<p>Hallo,</p>
<p>Was genau bedeutet (zahl &amp; (1 &lt;&lt; k)?</p>
</blockquote>
<p>Wenn bit k in zahl gesetzt ist dann ..., andernfalls ....</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2434659</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2434659</guid><dc:creator><![CDATA[unreg]]></dc:creator><pubDate>Thu, 25 Dec 2014 12:36:39 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu Bitverschiebung on Sun, 28 Dec 2014 06:55:01 GMT]]></title><description><![CDATA[<p>Wie kommt der Herr Breymann darauf, dass ein Byte immer 8 Bits hat?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2434921</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2434921</guid><dc:creator><![CDATA[HargeFrage]]></dc:creator><pubDate>Sun, 28 Dec 2014 06:55:01 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu Bitverschiebung on Sun, 28 Dec 2014 08:17:16 GMT]]></title><description><![CDATA[<p>HargeFrage schrieb:</p>
<blockquote>
<p>Wie kommt der Herr Breymann darauf, dass ein Byte immer 8 Bits hat?</p>
</blockquote>
<p>Vermutlich weiß er das schon, aber er ist kein c-plusplus.net-Forenklugscheißer, der in jeder auch noch so nebensächlichen Aussage nach irgendwelchen Ungenauigkeiten sucht, deren Erwähnung völlig an der Sache vorbeigehen würde.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2434928</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2434928</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sun, 28 Dec 2014 08:17:16 GMT</pubDate></item></channel></rss>