<?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[vector&amp;lt;char&amp;gt; mit beliebiger groesse um x bit&#x27;s nach links rotieren]]></title><description><![CDATA[<p>Hi, ich moechte einen byte vector 1 bit nachts links rotieren...die funktion hat noch einen bug...den ich nicht finde:</p>
<p>Ausgabe in Binary:</p>
<pre><code>0000000000000001
0000000000000010
0000000000000100
0000000000001000
0000000000010000
0000000000100000
0000000001000000
0000000010000000
0100000000000000
1000000000000000
0000000000000001
0000000000000010
0000000000000100
0000000000001000
0000000000010000
0000000000100000
0000000001000000
0000000010000000
0100000000000000
1000000000000000
</code></pre>
<p>Code:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;
#include &lt;iomanip&gt;
#include &lt;algorithm&gt;
using namespace std;

typedef char BYTE;

void rotate_1bit_left(vector&lt;BYTE&gt; &amp;array) {
    if(array.size() == 0) {
		return;
	}

    BYTE shifted = 0x00;    
    BYTE overflow = (0x80 &amp; array[0]) &gt;&gt; 7;

    for (int i = (array.size() - 1); i &gt;= 0; i--) {	
        shifted = (array[i] &lt;&lt; 1) | overflow;
        overflow = (0x80 &amp; array[i]) &gt;&gt; 1;
        array[i] = shifted;
    }
}

void rotate_bits_left(vector&lt;BYTE&gt; &amp;array, unsigned int number_of_bits) {
    for(int i = 0; i &lt; number_of_bits; i++) { 
        rotate_1bit_left(array);
    }
}

string char_to_binary(unsigned char c) {
    string s;
    int x = (int)c;

    do
    {
        s.push_back('0' + (x &amp; 1));
    } while (x &gt;&gt;= 1);

    while(s.size() &lt; 8) { 
        s = s + '0';
    }

    std::reverse(s.begin(), s.end());

    return s;
}

string char_to_hex(unsigned char c) {
    stringstream stream;

	stream &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; hex &lt;&lt; (int)c;

	return stream.str();
}

string get_hex_string(const vector&lt;BYTE&gt; &amp;vec) {
    string out;

    for(int i = 0; i &lt; vec.size(); i++) {
        out += char_to_hex(vec[i]);
    }

    return out;
}

string get_binary_string(const vector&lt;BYTE&gt; &amp;vec) {
    string out;

    for(int i = 0; i &lt; vec.size(); i++) {
        out += char_to_binary(vec[i]);
    }

    return out;
}

int main() {
	// your code goes here

	vector&lt;BYTE&gt; key = {0x00, 0x01};
	const int number_of_bits = 20;

	for(int i = 0; i &lt; number_of_bits; i++) {
		cout &lt;&lt; get_binary_string(key) &lt;&lt; endl;

		rotate_bits_left(key, 1); // koennen auch 20 bits sein...das muss dann umgemappt werden, wie gross der vector halt ist...
	}

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/323450/vector-lt-char-gt-mit-beliebiger-groesse-um-x-bit-s-nach-links-rotieren</link><generator>RSS for Node</generator><lastBuildDate>Thu, 16 Jul 2026 02:44:00 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/323450.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 30 Jan 2014 22:02:08 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to vector&amp;lt;char&amp;gt; mit beliebiger groesse um x bit&#x27;s nach links rotieren on Thu, 30 Jan 2014 22:02:08 GMT]]></title><description><![CDATA[<p>Hi, ich moechte einen byte vector 1 bit nachts links rotieren...die funktion hat noch einen bug...den ich nicht finde:</p>
<p>Ausgabe in Binary:</p>
<pre><code>0000000000000001
0000000000000010
0000000000000100
0000000000001000
0000000000010000
0000000000100000
0000000001000000
0000000010000000
0100000000000000
1000000000000000
0000000000000001
0000000000000010
0000000000000100
0000000000001000
0000000000010000
0000000000100000
0000000001000000
0000000010000000
0100000000000000
1000000000000000
</code></pre>
<p>Code:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;
#include &lt;iomanip&gt;
#include &lt;algorithm&gt;
using namespace std;

typedef char BYTE;

void rotate_1bit_left(vector&lt;BYTE&gt; &amp;array) {
    if(array.size() == 0) {
		return;
	}

    BYTE shifted = 0x00;    
    BYTE overflow = (0x80 &amp; array[0]) &gt;&gt; 7;

    for (int i = (array.size() - 1); i &gt;= 0; i--) {	
        shifted = (array[i] &lt;&lt; 1) | overflow;
        overflow = (0x80 &amp; array[i]) &gt;&gt; 1;
        array[i] = shifted;
    }
}

void rotate_bits_left(vector&lt;BYTE&gt; &amp;array, unsigned int number_of_bits) {
    for(int i = 0; i &lt; number_of_bits; i++) { 
        rotate_1bit_left(array);
    }
}

string char_to_binary(unsigned char c) {
    string s;
    int x = (int)c;

    do
    {
        s.push_back('0' + (x &amp; 1));
    } while (x &gt;&gt;= 1);

    while(s.size() &lt; 8) { 
        s = s + '0';
    }

    std::reverse(s.begin(), s.end());

    return s;
}

string char_to_hex(unsigned char c) {
    stringstream stream;

	stream &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; hex &lt;&lt; (int)c;

	return stream.str();
}

string get_hex_string(const vector&lt;BYTE&gt; &amp;vec) {
    string out;

    for(int i = 0; i &lt; vec.size(); i++) {
        out += char_to_hex(vec[i]);
    }

    return out;
}

string get_binary_string(const vector&lt;BYTE&gt; &amp;vec) {
    string out;

    for(int i = 0; i &lt; vec.size(); i++) {
        out += char_to_binary(vec[i]);
    }

    return out;
}

int main() {
	// your code goes here

	vector&lt;BYTE&gt; key = {0x00, 0x01};
	const int number_of_bits = 20;

	for(int i = 0; i &lt; number_of_bits; i++) {
		cout &lt;&lt; get_binary_string(key) &lt;&lt; endl;

		rotate_bits_left(key, 1); // koennen auch 20 bits sein...das muss dann umgemappt werden, wie gross der vector halt ist...
	}

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2380678</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2380678</guid><dc:creator><![CDATA[algoman1]]></dc:creator><pubDate>Thu, 30 Jan 2014 22:02:08 GMT</pubDate></item><item><title><![CDATA[Reply to vector&amp;lt;char&amp;gt; mit beliebiger groesse um x bit&#x27;s nach links rotieren on Thu, 30 Jan 2014 22:06:55 GMT]]></title><description><![CDATA[<p>std::rotate ist dein Freund.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2380679</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2380679</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Thu, 30 Jan 2014 22:06:55 GMT</pubDate></item><item><title><![CDATA[Reply to vector&amp;lt;char&amp;gt; mit beliebiger groesse um x bit&#x27;s nach links rotieren on Thu, 30 Jan 2014 22:10:52 GMT]]></title><description><![CDATA[<p>rotate(vec.begin()+1, vec.begin(), vec.end()) ... rotiert mir aber 1 byte nach links</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2380680</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2380680</guid><dc:creator><![CDATA[algoman1]]></dc:creator><pubDate>Thu, 30 Jan 2014 22:10:52 GMT</pubDate></item><item><title><![CDATA[Reply to vector&amp;lt;char&amp;gt; mit beliebiger groesse um x bit&#x27;s nach links rotieren on Thu, 30 Jan 2014 22:20:33 GMT]]></title><description><![CDATA[<p>Mein erster Gedanke dazu:<br />
In Zeile 21 shiftest Du um 1 statt um 7.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2380681</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2380681</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Thu, 30 Jan 2014 22:20:33 GMT</pubDate></item><item><title><![CDATA[Reply to vector&amp;lt;char&amp;gt; mit beliebiger groesse um x bit&#x27;s nach links rotieren on Thu, 30 Jan 2014 22:51:21 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/106">@volkard</a>: danke...das war der fehler <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 das mit std::rotate implementieren....wird der code schneller?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2380684</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2380684</guid><dc:creator><![CDATA[algoman1]]></dc:creator><pubDate>Thu, 30 Jan 2014 22:51:21 GMT</pubDate></item><item><title><![CDATA[Reply to vector&amp;lt;char&amp;gt; mit beliebiger groesse um x bit&#x27;s nach links rotieren on Thu, 30 Jan 2014 23:00:35 GMT]]></title><description><![CDATA[<p>algoman1 schrieb:</p>
<blockquote>
<p>rotate(vec.begin()+1, vec.begin(), vec.end()) ... rotiert mir aber 1 byte nach links</p>
</blockquote>
<p>Richtig wäre rotate(vec.begin(), vec.begin()+1, vec.end())</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2380686</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2380686</guid><dc:creator><![CDATA[nachrechts]]></dc:creator><pubDate>Thu, 30 Jan 2014 23:00:35 GMT</pubDate></item><item><title><![CDATA[Reply to vector&amp;lt;char&amp;gt; mit beliebiger groesse um x bit&#x27;s nach links rotieren on Thu, 30 Jan 2014 23:24:19 GMT]]></title><description><![CDATA[<p>algoman1 schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/106">@volkard</a>: danke...das war der fehler <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="🙂"
    /><br />
wie kann ich das mit std::rotate implementieren....wird der code schneller?</p>
</blockquote>
<p>Ähm, eigentlich nicht.<br />
std::rotate rotiert byteweise, wie Du schon gesagt hast.<br />
Du willst aber bitweise rotieren.<br />
Das ist, ähm, ja völlig was aderes.</p>
<p>Aber wenn Du Speed brauchst, kannst doch es benutzen!</p>
<p>Mit std::rotate tust Du byteweises Rotieren implementieren.</p>
<pre><code>void rotate_1_bit_left(vector&lt;BYTE&gt; &amp;array);//wie gehabt
void rotate_n_byte_left(vector&lt;BYTE&gt; &amp;array);//mit std::rotate
void rotate_bits_left(vector&lt;BYTE&gt; &amp;array, unsigned int number_of_bits) {
   unsigned int number_of_bytes=number_of_bits/8;
   unsigned int number_of_bits_left=number_of_bits%8;
   rotate_n_byte_left(array,number_of_bytes);
   for(...)
      void rotate_1_bit_left(...);      
}
</code></pre>
<p>Damit haste egal um wieviele Bits rotiert werden soll maximal einmal std::rotate und maximal 7-mal rotate_1byte_left, zusammen lineare Laufzeit.</p>
<p>Kannst dann noch, vermute ich, aber dazu gehört ein wenig Anstrengung, aber kriegste sicher hin, auch wenns einen Tag dauert,</p>
<pre><code>void rotate_n_bit_left(array,n)//0&lt;=n&lt;=7
</code></pre>
<p>basteln, was nur einmal übers ganzte Array durchläuft. Musst ja nicht &lt;&lt;1 machen, &lt;&lt;n geht ja auch. Entsprechend die Maske anpassen, <strong>const unsingend int maske=0xff&lt;&lt;(8-n)</strong> oder so. Dann wärst Du mit zwei Durchläufen durchs Array immer bedient, kackegal wie groß oder krumm n ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2380689</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2380689</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Thu, 30 Jan 2014 23:24:19 GMT</pubDate></item><item><title><![CDATA[Reply to vector&amp;lt;char&amp;gt; mit beliebiger groesse um x bit&#x27;s nach links rotieren on Thu, 30 Jan 2014 23:57:42 GMT]]></title><description><![CDATA[<p>kann man das eventuelle mit sse intrinsic's mit mehr speed implementieren?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2380698</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2380698</guid><dc:creator><![CDATA[algoman1]]></dc:creator><pubDate>Thu, 30 Jan 2014 23:57:42 GMT</pubDate></item><item><title><![CDATA[Reply to vector&amp;lt;char&amp;gt; mit beliebiger groesse um x bit&#x27;s nach links rotieren on Fri, 31 Jan 2014 01:03:20 GMT]]></title><description><![CDATA[<p>algoman1 schrieb:</p>
<blockquote>
<p>kann man das eventuelle mit sse intrinsic's mit mehr speed implementieren?</p>
</blockquote>
<p>Auswendig weiß ich es leider nicht. Bei meinen Problemen tut der optimierende Compiler stets SSE so ungeheuer schlau einbauen, daß ich gar keine Chance habe, es per Hand besser zu machen, das Compilat achtet drauf, Register gerade so zu benutzen und zuz belegen, daß möglichst viel parallel passieren kann. Das ist weit jenseits und überlegen des einfachen Takte-Sparens.</p>
<p>Im vorliegenden Fall würde ich zuerst versuchen, den Datentyp größer zu wählen, sagen wie mal unsigned long long statt unsinged char. Und mal schauen, was passiert. Ok, ull ist nur 64-bittig. SSE-Register können mehr. Aber die Befehle drauf sind immer nur für kleine Einheiten. Um damit auf eine ganzes Shift zu kommen, müßte man bestimmt noch ein wenig bit twiddeling betreiben, das frißt die Performance auf, denke ich mal.</p>
<p>Also meine Empfehlung aus dem Bauch heraus: Mit ull kurz drüberhpsen und den Rest schnell kleiner machen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2380711</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2380711</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 31 Jan 2014 01:03:20 GMT</pubDate></item></channel></rss>