<?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[Bytearray schieben]]></title><description><![CDATA[<p>Hallo <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>
<p>Wie kann ich alle Bytes eines Bytearrays um n bits schieben?<br />
Also es gehören immer 4 Bytes zusammen, und sollen die form 0x00016001 haben.<br />
Es kommt aber vor das die Bits verschoben sind. Z.B. so dass die erste 4Byte sequenz des Arrays (bei 4 geschobenen bits) so aussieht: 0x100001600.</p>
<p>Ich hatte überlegt das Bytearray bit für bit auszulesen und vorne so lange bits wegzuschneiden bis ich am ende der 6001 sequenz bin, damit dann ganz sicher eine vollständige gewollte sequenz am anfang steht. Danach hätte ich wieder alles in ein Array geschrieben.</p>
<p>Aber das scheint mir sehr umständlich. Hat jemand eine bessere Idee?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/319049/bytearray-schieben</link><generator>RSS for Node</generator><lastBuildDate>Sun, 26 Jul 2026 07:41:03 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/319049.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 05 Aug 2013 09:35:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Bytearray schieben on Mon, 05 Aug 2013 09:35:58 GMT]]></title><description><![CDATA[<p>Hallo <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>
<p>Wie kann ich alle Bytes eines Bytearrays um n bits schieben?<br />
Also es gehören immer 4 Bytes zusammen, und sollen die form 0x00016001 haben.<br />
Es kommt aber vor das die Bits verschoben sind. Z.B. so dass die erste 4Byte sequenz des Arrays (bei 4 geschobenen bits) so aussieht: 0x100001600.</p>
<p>Ich hatte überlegt das Bytearray bit für bit auszulesen und vorne so lange bits wegzuschneiden bis ich am ende der 6001 sequenz bin, damit dann ganz sicher eine vollständige gewollte sequenz am anfang steht. Danach hätte ich wieder alles in ein Array geschrieben.</p>
<p>Aber das scheint mir sehr umständlich. Hat jemand eine bessere Idee?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343515</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343515</guid><dc:creator><![CDATA[cl90]]></dc:creator><pubDate>Mon, 05 Aug 2013 09:35:58 GMT</pubDate></item><item><title><![CDATA[Reply to Bytearray schieben on Mon, 05 Aug 2013 09:39:22 GMT]]></title><description><![CDATA[<p>ungetestet:</p>
<pre><code>int main()
{
    unsigned char data[4];
    (*reinterpret_cast&lt;unsigned long*&gt;(data)) &lt;&lt;= 1;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2343516</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343516</guid><dc:creator><![CDATA[dr. bastel]]></dc:creator><pubDate>Mon, 05 Aug 2013 09:39:22 GMT</pubDate></item><item><title><![CDATA[Reply to Bytearray schieben on Mon, 05 Aug 2013 09:50:44 GMT]]></title><description><![CDATA[<p>Könnte ich damit auch das ganze Array um 1 bit shiften?<br />
Das wäre eine ziemlich Effektive Lösung! <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/2343517</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343517</guid><dc:creator><![CDATA[cl90]]></dc:creator><pubDate>Mon, 05 Aug 2013 09:50:44 GMT</pubDate></item><item><title><![CDATA[Reply to Bytearray schieben on Mon, 05 Aug 2013 10:49:24 GMT]]></title><description><![CDATA[<blockquote>
<p>Könnte ich damit auch das ganze Array um 1 bit shiften?</p>
</blockquote>
<p>Nein, die Lösung funktioniert nur solange das Array von der Gesamtgröße her nicht größer ist als ein Skalar.</p>
<p>Deine Lösung sieht anders aus.</p>
<pre><code>#include &lt;iostream&gt;

template&lt;typename T,
         std::size_t N&gt;
void rshift_arr( T(&amp;arr)[N], std::size_t to_shift )
{
	auto bytes = reinterpret_cast&lt;unsigned char*&gt;(arr);
	auto index = N * sizeof(T);
	while( index-- )
	{
		bytes[index] &gt;&gt;= to_shift;

		if( index )
			bytes[index] |= (bytes[index - 1] &lt;&lt; (CHAR_BIT - to_shift));
	}
}

#include &lt;bitset&gt;

template&lt;std::size_t N&gt;
void dump( char(&amp;a)[N] )
{
	for( auto c : a )
		std::cout &lt;&lt; std::bitset&lt;CHAR_BIT&gt;(c);
	std::cout &lt;&lt; '\n';
}

int main()
{
	char arr[10];
	dump(arr);
	rshift_arr(arr, 1);
	dump(arr);
}
</code></pre>
<p><code>rshift_arr</code> kann man noch stark optimieren, in dem man für bestimmte Array- und Typ-Größen spezielle Algorithmen mit größeren Zwischenwerttypen nimmt, bspw. <code>uint32_t</code> oder auch 64-Bit.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343530</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343530</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Mon, 05 Aug 2013 10:49:24 GMT</pubDate></item><item><title><![CDATA[Reply to Bytearray schieben on Mon, 05 Aug 2013 11:09:29 GMT]]></title><description><![CDATA[<p>Das sieht vielversprechend aus.</p>
<p>Ich muss ja noch herrausfinden wie viele bits mein Array verschoben ist, ich hab mir da folgendes überlegt, leider funktioniert es nicht:</p>
<pre><code>// Die einträge von &quot;bytes&quot; sind unsigned shorts, also bereits 16 bit
		for(int offset = 0; offset &lt; 20; offset++)
		{
			if(0x0001 == (bytes[0] &lt;&lt; offset) || (bytes[1] &gt;&gt; (16-offset))) break;
		}
</code></pre>
<p>compiler sagt:</p>
<pre><code>error C2678: Binärer Operator '&lt;&lt;': Es konnte kein Operator gefunden werden,
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2343534</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343534</guid><dc:creator><![CDATA[cl90]]></dc:creator><pubDate>Mon, 05 Aug 2013 11:09:29 GMT</pubDate></item><item><title><![CDATA[Reply to Bytearray schieben on Mon, 05 Aug 2013 11:31:13 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">template&lt;typename T,
         std::size_t N&gt;
void rshift_arr( T(&amp;arr)[N], std::size_t to_shift )
{
    auto bytes = reinterpret_cast&lt;unsigned char*&gt;(arr);
    auto index = N * sizeof(T);
    while( index-- )
    {
        bytes[index] &gt;&gt;= to_shift;

        if( index )
            bytes[index] |= (bytes[index - 1] &lt;&lt; (CHAR_BIT - to_shift));
    }
}
</code></pre>
<p>Worst Code ever!<br />
1. Gaukelt vor, ein Template zu sein, aber alles was es mit den Typen macht ist nur ein reinterpret_cast&lt;unsigned char*&gt; (lol).<br />
2. Geht nur für Arrays, dessen Länge zur Compile-Time feststeht<br />
3. Es ist nicht klar, weshalb size_t, das legt nahe, to_shift könne beliebig gross sein (aber warum nicht negativ? Ein memmove reicht um to_shift zwischen 0 und CHAR_BIT zu bringen)<br />
4. Sehr wirre Art, eine for-Loop zu schreiben<br />
5. Ein sehr ineffiziente Art, eine for-Loop zu schreiben</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343541</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343541</guid><dc:creator><![CDATA[ev4r]]></dc:creator><pubDate>Mon, 05 Aug 2013 11:31:13 GMT</pubDate></item><item><title><![CDATA[Reply to Bytearray schieben on Mon, 05 Aug 2013 11:43:16 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>Nein, die Lösung funktioniert nur solange das Array von der Gesamtgröße her nicht größer ist als ein Skalar.</p>
</blockquote>
<p>cl90 schrieb:</p>
<blockquote>
<p>Also es gehören immer 4 Bytes zusammen,[...]</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343544</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343544</guid><dc:creator><![CDATA[dr. bastel]]></dc:creator><pubDate>Mon, 05 Aug 2013 11:43:16 GMT</pubDate></item><item><title><![CDATA[Reply to Bytearray schieben on Mon, 05 Aug 2013 11:48:49 GMT]]></title><description><![CDATA[<p>Denkt da dran, dass ein Shift einer 4-Byte Zahl ganz ein anderes Ergebnis haben kann als ein Shift von 4 Mal einem Byte. Endianess und so.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343549</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343549</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 05 Aug 2013 11:48:49 GMT</pubDate></item><item><title><![CDATA[Reply to Bytearray schieben on Mon, 05 Aug 2013 12:38:08 GMT]]></title><description><![CDATA[<p>Worst argumentation ever!</p>
<blockquote>
<p>1. Gaukelt vor, ein Template zu sein, aber alles was es mit den Typen macht ist nur ein reinterpret_cast&lt;unsigned char*&gt; (lol).</p>
</blockquote>
<p>Kann man natürlich umschreiben, das war nur der Bequemlichkeit des Beispiels wegen.</p>
<blockquote>
<p>2. Geht nur für Arrays, dessen Länge zur Compile-Time feststeht</p>
</blockquote>
<p>Nein, die Lösung lässt sich sehr leicht anpassen, s.o.</p>
<blockquote>
<p>3. Es ist nicht klar, weshalb size_t, das legt nahe, to_shift könne beliebig gross sein</p>
</blockquote>
<p>Nein, könnte es nicht. Es macht keinen Sinn.</p>
<blockquote>
<p>aber warum nicht negativ?</p>
</blockquote>
<p>Weil es so nicht geht. Das Prinzip lässt sich nicht mit einer negativen shift-Breite in einen links-Shift umwandeln, deswegen heißt es explizit <strong>r</strong>shift.<br />
Btw, man merkt, was für eine Sorte du bist:</p>
<p>N3690 §5.8/1 schrieb:</p>
<blockquote>
<p>The behavior is undefined if the right operand<br />
is negative</p>
</blockquote>
<blockquote>
<p>Ein memmove reicht um to_shift zwischen 0 und CHAR_BIT zu bringen</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
<blockquote>
<p>4. Sehr wirre Art, eine for-Loop zu schreiben</p>
</blockquote>
<p>Geschmackssache. Zeig mir, wie du es schöner machst - nicht einfach anders.</p>
<blockquote>
<p>5. Ein sehr ineffiziente Art, eine for-Loop zu schreiben</p>
</blockquote>
<p>So? Schreibe dieselbe for-loop um und zeige mir, dass es effizienter ist.<br />
Klar, man könnte</p>
<pre><code>while( index-- )
    {
        bytes[index] &gt;&gt;= to_shift;

        if( index )
            bytes[index] |= (bytes[index - 1] &lt;&lt; (CHAR_BIT - to_shift));
    }
</code></pre>
<p>Umschreiben zu</p>
<pre><code>while( index-- &gt; 1 )
    {
        bytes[index] &gt;&gt;= to_shift;
        bytes[index] |= (bytes[index - 1] &lt;&lt; (CHAR_BIT - to_shift));
    }

    bytes[0] &gt;&gt;= to_shift;
</code></pre>
<p>Ich ging jetzt davon aus, dass der Compiler das alleine schafft.</p>
<pre><code>void rshift_arr( unsigned char* bytes,
				 std::size_t length,
				 std::size_t to_shift )
{
	while( length-- )
	{
		bytes[length] &gt;&gt;= to_shift;

		if( length )
			bytes[length] |= (bytes[length - 1] &lt;&lt; (CHAR_BIT - to_shift));
	}
}
</code></pre>
<p>Deine ersten Beiden Punkte fallen weg, ohne den Algorithmus überhaupt nur anzufassen...<br />
der Rest scheint mir dazu erfunden um mich noch mehr zu kritisieren. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_down"
      title=":-1:"
      alt="👎"
    /></p>
<p>dr. bastel schrieb:</p>
<blockquote>
<p>Sone schrieb:</p>
<blockquote>
<p>Nein, die Lösung funktioniert nur solange das Array von der Gesamtgröße her nicht größer ist als ein Skalar.</p>
</blockquote>
<p>cl90 schrieb:</p>
<blockquote>
<p>Also es gehören immer 4 Bytes zusammen,[...]</p>
</blockquote>
</blockquote>
<p><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="🙂"
    /> Habe den OP nicht richtig angeguckt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343553</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343553</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Mon, 05 Aug 2013 12:38:08 GMT</pubDate></item><item><title><![CDATA[Reply to Bytearray schieben on Mon, 05 Aug 2013 13:16:14 GMT]]></title><description><![CDATA[<blockquote>
<p>Weil es so nicht geht. Das Prinzip lässt sich nicht mit einer negativen shift-Breite in einen links-Shift umwandeln, deswegen heißt es explizit rshift.</p>
</blockquote>
<p>Man merkt, was für eine Sorte du bist.</p>
<p>Ich spielte darauf ab, dass du <code>assert(to_shift &lt; CHAR_BIT)</code> hinschreiben sollst, weil die Funktion sonst garantiert von irgendjemandem falsch verwendet wird. Also quasi</p>
<pre><code>void bitshift(unsigned char* s, size_t n, unsigned rshift)
{
  assert(rshift&lt;CHAR_BIT);

  unsigned char carry=0,tmp;
  for (size_t i=rshift?0:n; i&lt;n; ++i) {
    tmp = carry; carry = s[i]&lt;&lt;(CHAR_BIT-rshift);
    s[i] = tmp | (s[i]&gt;&gt;rshift);
  }
}
</code></pre>
<p>Aber warum &lt;CHAR_BIT? Die Einschränkung lässt sich leicht beheben:</p>
<pre><code>void bitmove(unsigned char* s, size_t n, int rshift)
{
  if (rshift &gt;= CHAR_BIT) {
    unsigned m = rshift/CHAR_BIT; rshift %= CHAR_BIT;
    memmove(s+m, s, n - m);
    memset(s, 0, m);
  } else if (rshift &lt; 0) {
    unsigned m = (CHAR_BIT-1-rshift)/CHAR_BIT;
    rshift = (CHAR_BIT + (rshift%CHAR_BIT))%CHAR_BIT;
    memmove(s, s+m, n - m);
    memset(s+m-n, 0, m);
  }
  bitshift(s, n, rshift);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2343579</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343579</guid><dc:creator><![CDATA[ev4r]]></dc:creator><pubDate>Mon, 05 Aug 2013 13:16:14 GMT</pubDate></item><item><title><![CDATA[Reply to Bytearray schieben on Tue, 06 Aug 2013 03:41:00 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>Klar, man könnte ... Umschreiben zu ...</p>
</blockquote>
<p>lieber</p>
<pre><code>for(auto index = N * sizeof(T); index; --index) {
    bytes[index] &gt;&gt;= to_shift;
    bytes[index] |= (bytes[index - 1] &lt;&lt; (CHAR_BIT - to_shift));
}
</code></pre>
<p>Deine if-abfrage in der While-Schleife ergibt immer true</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343761</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343761</guid><dc:creator><![CDATA[Cyres]]></dc:creator><pubDate>Tue, 06 Aug 2013 03:41:00 GMT</pubDate></item><item><title><![CDATA[Reply to Bytearray schieben on Tue, 06 Aug 2013 06:45:01 GMT]]></title><description><![CDATA[<p>Cyres schrieb:</p>
<blockquote>
<p>Deine if-abfrage in der While-Schleife ergibt immer true</p>
</blockquote>
<p>Nein, das tut sie nicht, denkst du dann würde sie da stehen? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /><br />
Schau mal genau hin.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343784</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343784</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Tue, 06 Aug 2013 06:45:01 GMT</pubDate></item><item><title><![CDATA[Reply to Bytearray schieben on Tue, 06 Aug 2013 06:51:06 GMT]]></title><description><![CDATA[<p>Cyres schrieb:</p>
<blockquote>
<p>Deine if-abfrage in der While-Schleife ergibt immer true</p>
</blockquote>
<p>tut sie nicht, <strong>post</strong>dekrement und so...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2343787</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343787</guid><dc:creator><![CDATA[unterregistrierter]]></dc:creator><pubDate>Tue, 06 Aug 2013 06:51:06 GMT</pubDate></item><item><title><![CDATA[Reply to Bytearray schieben on Tue, 06 Aug 2013 07:53:54 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>Cyres schrieb:</p>
<blockquote>
<p>Deine if-abfrage in der While-Schleife ergibt immer true</p>
</blockquote>
<p>Nein, das tut sie nicht, denkst du dann würde sie da stehen? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /><br />
Schau mal genau hin.</p>
</blockquote>
<p>narf, ich sollte mir abgewöhnen mitten in der Nacht Posts zu schreiben <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2343805</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2343805</guid><dc:creator><![CDATA[Cyres]]></dc:creator><pubDate>Tue, 06 Aug 2013 07:53:54 GMT</pubDate></item></channel></rss>