<?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[Hilfe bei Aufgabe aus &amp;quot;Der C++ Programmierer&amp;quot;]]></title><description><![CDATA[<p>Hallo</p>
<p>Ich lerne gerade C++ mit dem Buch Der C++ Programmier von Ulrich Breymann.<br />
Leider habe ich schon bei der zweiten Aufgabe nicht verstanden was man genau machen muss.<br />
Kann mir das jemand genauer erklären ?</p>
<blockquote>
<p>Schreiben Sie ein Programm, das die grösstmögliche unsigned int Zahl(int, long, unsigned long) ausgibt, ohne dass die Kenntnis der systeminternen verwendeten Bitanzahl für jeden Datentyp benutzt wird. Hinweis: studieren Sie die möglichen Operatoren für ganze Zahlen und die Datei limits.h</p>
</blockquote>
<p>Das ist die Lösung:</p>
<pre><code class="language-cpp">/* cppbuch/loesungen/k1/2.cpp
   Beispiel zum Buch von Ulrich Breymann: Der C++ Programmierer; Hanser Verlag
   Diese Software ist freie Software. Website zum Buch: http://www.cppbuch.de/ 
*/
#include&lt;iostream&gt;
using namespace std;

int main() {
   unsigned int ui = 0;
   unsigned long int uli = 0;
   cout &lt;&lt; &quot;max. unsigned int     = &quot;&lt;&lt; ~ui       &lt;&lt; endl;
   cout &lt;&lt; &quot;max. unsigned long int= &quot;&lt;&lt; ~uli      &lt;&lt; endl;
   cout &lt;&lt; &quot;max. int              = &quot;&lt;&lt; (~ui&gt;&gt;1)  &lt;&lt; endl;
   cout &lt;&lt; &quot;max. long int         = &quot;&lt;&lt; (~uli&gt;&gt;1) &lt;&lt; endl;
}
</code></pre>
<p>Ich verstehe auch nicht genau was eine bitweise Negation ist.<br />
Es ist dieses Zeichen -&gt; ~<br />
Im Buch wurde es nicht genau erklärt nur kurz angegeben.<br />
Die Lösung verstehe ich nur wenig. Vor allem das Zeichen ~ und &quot;&gt;&gt; 1&quot; nicht.<br />
Es wäre nett wenn mir jemand ein wenig helfen könnte.</p>
<p>Vielen Dank</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/305045/hilfe-bei-aufgabe-aus-quot-der-c-programmierer-quot</link><generator>RSS for Node</generator><lastBuildDate>Fri, 26 Jun 2026 20:13:10 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/305045.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 19 Jun 2012 18:51:46 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Hilfe bei Aufgabe aus &amp;quot;Der C++ Programmierer&amp;quot; on Tue, 19 Jun 2012 18:51:46 GMT]]></title><description><![CDATA[<p>Hallo</p>
<p>Ich lerne gerade C++ mit dem Buch Der C++ Programmier von Ulrich Breymann.<br />
Leider habe ich schon bei der zweiten Aufgabe nicht verstanden was man genau machen muss.<br />
Kann mir das jemand genauer erklären ?</p>
<blockquote>
<p>Schreiben Sie ein Programm, das die grösstmögliche unsigned int Zahl(int, long, unsigned long) ausgibt, ohne dass die Kenntnis der systeminternen verwendeten Bitanzahl für jeden Datentyp benutzt wird. Hinweis: studieren Sie die möglichen Operatoren für ganze Zahlen und die Datei limits.h</p>
</blockquote>
<p>Das ist die Lösung:</p>
<pre><code class="language-cpp">/* cppbuch/loesungen/k1/2.cpp
   Beispiel zum Buch von Ulrich Breymann: Der C++ Programmierer; Hanser Verlag
   Diese Software ist freie Software. Website zum Buch: http://www.cppbuch.de/ 
*/
#include&lt;iostream&gt;
using namespace std;

int main() {
   unsigned int ui = 0;
   unsigned long int uli = 0;
   cout &lt;&lt; &quot;max. unsigned int     = &quot;&lt;&lt; ~ui       &lt;&lt; endl;
   cout &lt;&lt; &quot;max. unsigned long int= &quot;&lt;&lt; ~uli      &lt;&lt; endl;
   cout &lt;&lt; &quot;max. int              = &quot;&lt;&lt; (~ui&gt;&gt;1)  &lt;&lt; endl;
   cout &lt;&lt; &quot;max. long int         = &quot;&lt;&lt; (~uli&gt;&gt;1) &lt;&lt; endl;
}
</code></pre>
<p>Ich verstehe auch nicht genau was eine bitweise Negation ist.<br />
Es ist dieses Zeichen -&gt; ~<br />
Im Buch wurde es nicht genau erklärt nur kurz angegeben.<br />
Die Lösung verstehe ich nur wenig. Vor allem das Zeichen ~ und &quot;&gt;&gt; 1&quot; nicht.<br />
Es wäre nett wenn mir jemand ein wenig helfen könnte.</p>
<p>Vielen Dank</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2225140</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2225140</guid><dc:creator><![CDATA[Neuling._]]></dc:creator><pubDate>Tue, 19 Jun 2012 18:51:46 GMT</pubDate></item><item><title><![CDATA[Reply to Hilfe bei Aufgabe aus &amp;quot;Der C++ Programmierer&amp;quot; on Tue, 19 Jun 2012 19:39:51 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;bitset&gt;
#include &lt;iostream&gt;

using std::cout;
using std::bitset;

int main()
{

	long int l = 0;
	bitset&lt;32&gt; longset(l);
	cout &lt;&lt; longset.to_string() &lt;&lt; '\n';
	longset = ~longset;
	cout &lt;&lt; longset.to_string() &lt;&lt; '\n';
	cout &lt;&lt; longset.to_ulong() &lt;&lt; '\n';

}
</code></pre>
<p>Führ einmal dieses Programm aus. Es zeigt dir was der Operator ~ mit einer Zahl macht. (Normalerweise würde einen unsigned long nehmen, aber VC++ hat da einen Bug).</p>
<p>Hier nochmal erklärt:<br />
Das ist eine Binärzahl:<br />
0000 0000 = 0 Dezimal.<br />
Wenn wir Bitweises-Nicht mit der Zahl machen, wird jede 0 zu eins und jede 1 zu 0.</p>
<p>~(0000 0000) = 1111 1111 = 255<br />
~(1111 1111) = 0000 0000 = 0</p>
<p>Der &gt;&gt; Operator shiftet Bits = verändert die Position.<br />
Wenn du eine signed (vorzeichenbehaftete) Zahl hast, wird das linkste (= erste Bit) der Zahl für den negativen Wert verwendet. Wenn du also den maximalen Wert haben willst, muss das erste Bit weg, nachdem du es bitwise geNOTet hast.<br />
Wenn du &gt;&gt; 1 auf 1111 1111 machst, wird es zu 0111 1111<br />
Wenn du &gt;&gt; 1 auf 0110 0000 machst, wird es zu 0011 0000</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2225159</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2225159</guid><dc:creator><![CDATA[IrgendeinName]]></dc:creator><pubDate>Tue, 19 Jun 2012 19:39:51 GMT</pubDate></item><item><title><![CDATA[Reply to Hilfe bei Aufgabe aus &amp;quot;Der C++ Programmierer&amp;quot; on Tue, 19 Jun 2012 20:19:24 GMT]]></title><description><![CDATA[<p>Vielen Dank !<br />
Ich glaube ich habe es jetzt verstanden.<br />
Aber wie krieg ich die grösstmögliche einer int raus ?</p>
<p>So habe ich es versucht:</p>
<pre><code class="language-cpp">int a = 0;
a = (~a&gt;&gt;1);
</code></pre>
<p>Da krieg ich -1.<br />
1111 1111 1111 1111 1111 1111 = -1</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2225179</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2225179</guid><dc:creator><![CDATA[Neuling._]]></dc:creator><pubDate>Tue, 19 Jun 2012 20:19:24 GMT</pubDate></item><item><title><![CDATA[Reply to Hilfe bei Aufgabe aus &amp;quot;Der C++ Programmierer&amp;quot; on Tue, 19 Jun 2012 20:34:24 GMT]]></title><description><![CDATA[<p>Ich muss eine 0 'reinschieben', deswegen kam immer 1111 1111 statt 0111 1111.<br />
Stimmt das so ?</p>
<pre><code class="language-cpp">int a;
a = (~a&gt;&gt;0)
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2225187</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2225187</guid><dc:creator><![CDATA[Neuling._]]></dc:creator><pubDate>Tue, 19 Jun 2012 20:34:24 GMT</pubDate></item><item><title><![CDATA[Reply to Hilfe bei Aufgabe aus &amp;quot;Der C++ Programmierer&amp;quot; on Wed, 20 Jun 2012 06:03:53 GMT]]></title><description><![CDATA[<p>Neuling._ schrieb:</p>
<blockquote>
<p>Aber wie krieg ich die grösstmögliche einer int raus ?</p>
</blockquote>
<p>z.B. so:</p>
<pre><code class="language-cpp">int i = 1;
= ~(i &lt;&lt; (sizeof(int)*8-1));
cout &lt;&lt; &quot;max. int = &quot;&lt;&lt; i;
</code></pre>
<p>An dieser Stelle kann man auch noch erwähnen, dass <code>unsigned short int=0</code> negiert meist <code>-1</code> liefert:</p>
<blockquote>
<p>A prvalue of an integer type other than bool, char16_t, char32_t, or wchar_t whose integer conversion<br />
rank (4.13) is less than the rank of int can be converted to a prvalue of type int if int can represent all<br />
the values of the source type.</p>
</blockquote>
<blockquote>
<p>The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type.</p>
</blockquote>
<blockquote>
<p>The rank of a signed integer type shall be greater than the rank of any signed integer type with a<br />
smaller size.</p>
</blockquote>
<p>Beispiel:</p>
<pre><code class="language-cpp">unsigned short int ui = 0;
// unsigned short int --&gt; integral promotion --&gt; int
cout &lt;&lt; &quot;max. unsigned short int = &quot;&lt;&lt; ~ui; // int i=0 negiert ergibt -1.
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2225245</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2225245</guid><dc:creator><![CDATA[out]]></dc:creator><pubDate>Wed, 20 Jun 2012 06:03:53 GMT</pubDate></item><item><title><![CDATA[Reply to Hilfe bei Aufgabe aus &amp;quot;Der C++ Programmierer&amp;quot; on Wed, 20 Jun 2012 06:53:50 GMT]]></title><description><![CDATA[<p>[quote=&quot;out&quot;] z.B. so:</p>
<pre><code class="language-cpp">int i = 1;
= ~(i &lt;&lt; (sizeof(int)*8-1));
cout &lt;&lt; &quot;max. int = &quot;&lt;&lt; i;
</code></pre>
<p>[quote]</p>
<p>Danke ich glaube ich habe es verstanden.<br />
Mit sizeof(int) kriegt man die systeminterne Grösse von int raus (2 oder 4 byte), mit 8 multipliziert um in bits umzurechnen - 1 und verschiebt die Bits so um 15/ 31 stellen.<br />
Danach bekommt man die grösste negative und muss noch die Zahl negieren ?<br />
Ist das so richtig ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2225250</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2225250</guid><dc:creator><![CDATA[Neuling._.]]></dc:creator><pubDate>Wed, 20 Jun 2012 06:53:50 GMT</pubDate></item><item><title><![CDATA[Reply to Hilfe bei Aufgabe aus &amp;quot;Der C++ Programmierer&amp;quot; on Wed, 20 Jun 2012 06:54:57 GMT]]></title><description><![CDATA[<p>Neuling._ schrieb:</p>
<blockquote>
<p>Aber wie krieg ich die grösstmögliche einer int raus ?</p>
</blockquote>
<pre><code class="language-cpp">#include &lt;limits&gt;
std::numeric_limits&lt;int&gt;::max()

oder

#include &lt;climits&gt;
INT_MAX

oder

(unsigned int)-1&gt;&gt;1
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2225251</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2225251</guid><dc:creator><![CDATA[Wutz]]></dc:creator><pubDate>Wed, 20 Jun 2012 06:54:57 GMT</pubDate></item><item><title><![CDATA[Reply to Hilfe bei Aufgabe aus &amp;quot;Der C++ Programmierer&amp;quot; on Wed, 20 Jun 2012 07:22:02 GMT]]></title><description><![CDATA[<p>out schrieb:</p>
<blockquote>
<p>Neuling._ schrieb:</p>
<blockquote>
<p>Aber wie krieg ich die grösstmögliche einer int raus ?</p>
</blockquote>
<p>z.B. so:</p>
<pre><code class="language-cpp">int i = 1;
= ~(i &lt;&lt; (sizeof(int)*8-1));
cout &lt;&lt; &quot;max. int = &quot;&lt;&lt; i;
</code></pre>
</blockquote>
<p>hm...</p>
<blockquote>
<p>ohne dass die Kenntnis der systeminternen verwendeten Bitanzahl für jeden Datentyp benutzt wird</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2225256</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2225256</guid><dc:creator><![CDATA[daddy_felix]]></dc:creator><pubDate>Wed, 20 Jun 2012 07:22:02 GMT</pubDate></item></channel></rss>