<?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[nächstes vielfaches von 8]]></title><description><![CDATA[<p>ich suche eine möglichst elegante und effiziente möglichkeit eine zahl n ggf. zu erhöhen, so das sie anschließend ein vielfaches von 8 ist.</p>
<p>die zahl 12 soll auf 16 erhöht werden, 16 soll unverändert bleiben und 31 wird zu 32.</p>
<p>momentan mache ich es so:</p>
<pre><code class="language-cpp">uint32 n = ??;

if (n % 8 &gt; 0) {
	n += 8 - (n % 8);
}
</code></pre>
<p>gibt es eine elegantere lösung?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/259884/nächstes-vielfaches-von-8</link><generator>RSS for Node</generator><lastBuildDate>Tue, 08 Sep 2026 17:14:15 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/259884.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 28 Jan 2010 20:29:55 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to nächstes vielfaches von 8 on Thu, 28 Jan 2010 20:30:30 GMT]]></title><description><![CDATA[<p>ich suche eine möglichst elegante und effiziente möglichkeit eine zahl n ggf. zu erhöhen, so das sie anschließend ein vielfaches von 8 ist.</p>
<p>die zahl 12 soll auf 16 erhöht werden, 16 soll unverändert bleiben und 31 wird zu 32.</p>
<p>momentan mache ich es so:</p>
<pre><code class="language-cpp">uint32 n = ??;

if (n % 8 &gt; 0) {
	n += 8 - (n % 8);
}
</code></pre>
<p>gibt es eine elegantere lösung?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1846678</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1846678</guid><dc:creator><![CDATA[Sengha]]></dc:creator><pubDate>Thu, 28 Jan 2010 20:30:30 GMT</pubDate></item><item><title><![CDATA[Reply to nächstes vielfaches von 8 on Thu, 28 Jan 2010 20:33:06 GMT]]></title><description><![CDATA[<p>Sengha schrieb:</p>
<blockquote>
<p>ich suche eine möglichst elegante und effiziente möglichkeit eine zahl n ggf. zu erhöhen, so das sie anschließend ein vielfaches von 8 ist.</p>
<p>die zahl 12 soll auf 16 erhöht werden, 16 soll unverändert bleiben und 31 wird zu 32.</p>
</blockquote>
<pre><code class="language-cpp">unsigned next_multiple_of_eight(unsigned x)
{
  return (x + 7u) &amp; (~7u);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1846681</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1846681</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Thu, 28 Jan 2010 20:33:06 GMT</pubDate></item><item><title><![CDATA[Reply to nächstes vielfaches von 8 on Thu, 28 Jan 2010 20:35:07 GMT]]></title><description><![CDATA[<p>Ich mach es immer so. Ist leicht nachvollziehbar (finde ich) und ist auch nicht konditional.</p>
<pre><code class="language-cpp">uint32 n = ??;
n=(n+7)/8*8;
</code></pre>
<p>Die Division und Multiplikation wird bei Zweierpotenzen wie 8 automatisch mit performanten Bitshifts realisiert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1846682</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1846682</guid><dc:creator><![CDATA[Athar]]></dc:creator><pubDate>Thu, 28 Jan 2010 20:35:07 GMT</pubDate></item><item><title><![CDATA[Reply to nächstes vielfaches von 8 on Thu, 28 Jan 2010 20:36:14 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">int n = 31;
n &amp;= ~7;
n += 8;
</code></pre>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1846683</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1846683</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Thu, 28 Jan 2010 20:36:14 GMT</pubDate></item><item><title><![CDATA[Reply to nächstes vielfaches von 8 on Thu, 28 Jan 2010 20:40:22 GMT]]></title><description><![CDATA[<p>Dravere schrieb:</p>
<blockquote>
<pre><code class="language-cpp">int n = 31;
n &amp;= ~7;
n += 8;
</code></pre>
</blockquote>
<p>nicht 100% korrekt. Probier mal n=16</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1846686</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1846686</guid><dc:creator><![CDATA[kruemelkacker]]></dc:creator><pubDate>Thu, 28 Jan 2010 20:40:22 GMT</pubDate></item><item><title><![CDATA[Reply to nächstes vielfaches von 8 on Thu, 28 Jan 2010 20:44:48 GMT]]></title><description><![CDATA[<p>kruemelkacker schrieb:</p>
<blockquote>
<p>Dravere schrieb:</p>
<blockquote>
<pre><code class="language-cpp">int n = 31;
n &amp;= ~7;
n += 8;
</code></pre>
</blockquote>
<p>nicht 100% korrekt. Probier mal n=16</p>
</blockquote>
<p>Krümelkacker du *g*</p>
<pre><code class="language-cpp">int n = 16;
n += 7;
n &amp;= ~7;
</code></pre>
<p>Zufrieden? Ich habe etwas zu schnell gedacht <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>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1846695</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1846695</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Thu, 28 Jan 2010 20:44:48 GMT</pubDate></item></channel></rss>